Python3 中 input() 函数接受一个标准输入数据,返回为 string 类型。
Python2 中 input() 相等于 raw_input() ,用来获取控制台的输入。
raw_input() 将所有输入作为字符串看待,返回字符串类型。而 input() 在对待纯数字输入时具有自己的特性,它返回所输入的数字的类型( int, float )。
prompt = '\nTell me something,and I will repaet it back to you:'
prompt += "\n Enter 'quit' to end the program."
message =''
while message !='quit':
message = input(prompt)
if message != 'quit':
print(message)
Tell me something,and I will repaet it back to you:
Enter 'quit' to end the program.Hello everyone!
Hello everyone!
Tell me something,and I will repaet it back to you:
Enter 'quit' to end the program.Hello again!
Hello again!
Tell me something,and I will repaet it back to you:
Enter 'quit' to end the program.quit
>>>
prompt = '\nTell me something,and I will repaet it back to you:'
prompt += "\n Enter 'quit' to end the program."
message =''
active = True##让程序最初处于活动状态
while active:
message = input(prompt)
if message == 'quit':
active = False##这样做简化了while语句,因为不需要在其中做任何比较——相关的逻辑由程序的其他部分处理。只要变量active为True,循环就将继续运行。
##用户输入quit我们就将变量active设置为False,这将导致while循环不再继续执行。如果用户输入的不是quit,我们就将输入作为一条消息打印出来。
else:
print(message)
Tell me something,and I will repaet it back to you:
Enter 'quit' to end the program.
Hello
Hello
Tell me something,and I will repaet it back to you:
Enter 'quit' to end the program.nihao
nihao
Tell me something,and I will repaet it back to you:
Enter 'quit' to end the program.aaa
aaa
Tell me something,and I will repaet it back to you:
Enter 'quit' to end the program.quit
>>>
在要求很多条件都满足才继续运行的程序中,可定义一个变量,用于判断整个程序是否处于活动状态。这个变量被称为标志,充当了程序的交通信号灯。你可让程序在标志为True时继续运行,并在任何事件导致标志的值为False时让程序停止运行。这样,在while语句中就只需检查一个条件——标志的当前值是否为True,并将所有测试(是否发生了应将标志设置为False的事件)都放在其他地方,从而让程序变得更为整洁。
old_list_s = ['zhangsan','lisi','wangwu','zhaoliu']
new_list_s = []
while old_list_s:
Temporary_list_s = old_list_s.pop()
print('new user:'+Temporary_list_s.title())
new_list_s.append(Temporary_list_s)
print('\nExisting users have:')
for new_list in new_list_s:
print(new_list.title())
new user:Zhaoliu
new user:Wangwu
new user:Lisi
new user:Zhangsan
Existing users have:
Zhaoliu
Wangwu
Lisi
Zhangsan
>>>
def happy(name):
print("Hello",name.title()+'!')
name=input("What's your name?")
happy(name)
What's your name?hucheng
Hello Hucheng!
>>>
def name(first_name,last_name,age=''):
person ={'first_name':first_name,'last_name':last_name}
if age:
person['age'] = age
return person
people_name1 = name('hu','cheng')
people_name2 = name('li','hongfei',age=21)
print(people_name1)
print(people_name2)
>>>
{'first_name': 'hu', 'last_name': 'cheng'}
{'first_name': 'li', 'last_name': 'hongfei', 'age': 21}
>>>