==
!=
>
<
>=
<=
in
not in
and
or
True
False
if语句那行要有:
else语句那行也要有:
elif语句那行也要有:
else可以省略不写
for语句那边也要有:
prompt = 'If you tell us who you are, we can personalize the messages you see.'
prompt += "\nWhat is your name?"
name = input(prompt)
print("\nHello, " + name + "!")
用户输入都会被理解成一串字符串,以回车换行分隔
获取以空格分隔的多个输入数值
all = input().split(' ')
print(all)
for i,j in enumerate(all):
if j == '': #这里只是为了避免输入末尾有空格
continue
all[i] = int(j)
print(all)
不过当输入中有非数字字符时,将报错。
int(x,base)
base默认为10,表示字符串x为10进制,若指出base的值,x必须为字符串。
enumerate(sequence, [start=0])
将一个可遍历的数据对象(如列表、元组或字符串)组合为一个索引序列,同时列出数据和数据下标。sequence – 一个序列、迭代器或其他支持迭代对象;start – 下标起始位置,缺省为0。
while
while语句后面要记得跟:
prompt = '\nTell me something, and I will repeat it back to you:'
prompt += '\nEnter "quit" to end the program. '
messages = ""
while messages != "quit":
messages = input(prompt)
if messages != 'quit':
print(messages)
break
continue
可用
书本上的一个例子,看看体会一下
responses = {}
#设置一个标志,指出调查是否继续
polling_active = True
while polling_active:
# 提示输入被调查者的名字和回答
name = input('\nWhat is your name? ')
response = input('Which mountain would you like to climb someday?')
# 将答案存储在字典中
responses[name] = response
# 看看是否还有人要参与调查
repeat = input('Would you like to let another person respond? (yes/no)')
if repeat == 'no':
polling_active = False
# 调查结束,显示结果
print('\n--- Poll Results ---')
for name, response in responses.items():
print(name + ' would like to climb ' + response + '.')
如果觉得此文章有用,点击这里,万分感谢。
Reference
[美]Eric Matthes著,袁国忠译. Python编程从入门到实践[M]. 北京:人民邮电出版社. 2016.7.
Python函数-int()
Python enumerate() 函数