05笨方法学Python|ex27-ex34

2018-01-27  本文已影响13人  ericazhan

ex27

True 和False 的首字母必须大写。

ex28

Operators are special symbols in Python that carry out arithmetic or logical computation. The value that the operator operates on is called the operand.

Python Operators

ex29

写完if语句后,下一行print命令如果不缩进,就会报错。
写完要加冒号的东西,如function,if,里面的内容都需要TAB缩进,以此告诉电脑,这是一个block的。

ex 30

ex31

num = raw_input("give me a number.  ")
numm = float(num)
if numm < 0.:
    print "This is a negative."
elif numm in range(1,10):
    print "It's in the range of 1 to 10."
elif numm > 100:
    print "more than 100!"
else:
    print "Game over."

ex32

range(stop)
stop表示生成几个整数,“数目”
ex: range(3) ==[0,1,2]
range([start], stop[, step])
stop表示截止到哪个数,但不包括这个数;
step是间隔,如果不写,默认是1。
ex: range(0, -10, -2) == [0,-2,-4,-6,-8] #是的,可以是负数。

猜猜这个程序会输出什么结果?

for i in range(99, 0, -1):
        if i == 1:
                print "1 bottle of beer on the wall, 1 bottle of beer!"
                print 'So take it down, pass it around, no more bottles of beer on the wall!'
        elif i == 2:
                print '2 more bottles of beer on the wall, 2 more bottles of beer!'
                print 'So take one down, pass it around, 1 more bottle of beer on the wall!'
        else:
                print "%r bottles of beer on the wall, %r bottles of beer!" %(i,i)
                print "So take it down, pass it around, %r more bottles of beer on the wall!" %(i-1)

以上关于range的解释和题目来源于这个网页:pythoncentral

for i in range(5):
    a = i + 1
    print a
    
#-----这两个代码打印的结果很不一样!--------

for i in range(5):
    a = i + 1
print a

参考stackoverflow

for i in range(0,len_fruits):
    elements.append(i)
for i in elements:
    print elements[i]

ex33

附加题参考以下:
https://stackoverflow.com/a/24353790

ex34

ordinal number 序数词,如第一,第二,第三。。
cardinal number 基数词,如0,1,2,3。。。
注意,在python里,序数由0开始。所以0,对应列表list 里的第一。

#-*- coding:utf-8 -*-
goals = ["carrier","development","entertainment","finance","health","contribution","relationship"]

def plan(a):
    content = goals[a-1]
    print "The thing you want to do is of order %d, in the position %d." %(a,(a-1))

print "You have these choices for your next year."
print goals
choice = int(raw_input("What you want to do in the year 2018?          "))  
if choice in range(1,8):
    plan(choice)
else:
    print "You should give me a number from 1 to 6"

最近在排新年计划,正好顺手用了一下,哈~

上一篇 下一篇

猜你喜欢

热点阅读