Python 用户输入
函数input()接收一个参数,等待用户输入一些文本。获取用户输入后,可以将其存储在一个变量中,方便使用。
>>> message=input("Please input something:\n")
Please input something:
Hello,Python.
>>> print(message)
Hello,Python.
使用函数input()时,Python将用户输入解读为字符串。
函数**int()**将数字的字符串表示转换为数值表示。
>>> age=input("Please enter your age:\n")
Please enter your age:
20
>>> age=age+1
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can only concatenate str (not "int") to str
>>> age=int(age)
>>> age=age+1
>>> print(age)
21
用户输入和while循环
>>> prompt="\nTell me something ,and I will repeat 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 repeat it back to you :
Enter 'quit' to end the program.Hello,Jack
Hello,Jack
Tell me something ,and I will repeat it back to you :
Enter 'quit' to end the program.Welcome to Python
Welcome to Python
Tell me something ,and I will repeat it back to you :
Enter 'quit' to end the program.quit
while循环使用标志
在要求很多条件都满足才继续运行的程序中,可定义一个变量,用于判断整个程序是否处于活动状态。
>>> prompt="Please input something:\n"
>>> flag=True
>>> while flag:
... msg=input(prompt)
... if msg=='quit':
... flag=False
... else:
... print(msg)
...
Please input something:
Hello,Jack
Hello,Jack
Please input something:
Welcome to Python
Welcome to Python
Please input something:
quit
使用break()退出循环
要立即退出循环,不再运行循环中余下的代码,也不管条件测试的结果如何,可使用break语句。
>>> prompt="Please input your lucky numbers:"
>>> while True:
... msg=input(prompt)
... if msg=='quit':
... break
... else:
... print(msg)
...
Please input your lucky numbers:6
6
Please input your lucky numbers:8
8
Please input your lucky numbers:66
66
Please input your lucky numbers:quit
在循环中使用continue
>>> num=1
>>> while num<10:
... if num%2==0:
... num+=1
... continue
... else:
... print(num)
... num+=1
...
1
3
5
7
9
如果程序陷入无限循环,可以按Ctrl+C,也可以关闭显示程序输出的窗口。
for循环是一种遍历列表的有效方式,但在for循环中不应修改列表,否则导致Python难以跟踪其中的元素。要在遍历列表的同时对其进行修改,可使用while循环。通过while循环同列表和字典结合起来使用,可收集、存储并组织大量输入,供以后查看和显示。
在列表之间移动元素:
>>> color_1=["red","blue","green","yellow"]
>>> color_2=[]
>>> while color_1:
... color=color_1.pop()
... print("Take from color_1:"+color)
... color_2.append(color)
...
Take from color_1:yellow
Take from color_1:green
Take from color_1:blue
Take from color_1:red
>>> print(color_2)
['yellow', 'green', 'blue', 'red']
删除包含特定值的所有列表元素
>>> animals=["cat","dog","cat","pig","cat"]
>>> animals.remove("cat")
>>> print(animals)
['dog', 'cat', 'pig', 'cat']
>>> while "cat" in animals:
... animals.remove("cat")
...
>>> print(animals)
['dog', 'pig']
使用用户输入来填充字典
survey={}
>>> flag=True
>>> while flag:
... name=input("What is your name?\n")
... color=input("what is your favorite color?\n")
... survey[name]=color
... again=input("Anyone else?\n")
... if again.lower()=='n'or again.lower()=='no':
... flag=False
...
What is your name?
Jack
what is your favorite color?
blue
Anyone else?
y
What is your name?
Rose
what is your favorite color?
red
Anyone else?
n
>>> for name,color in survey.items():
... print(name+" favorite color is :"+color)
...
Jack favorite color is :blue
Rose favorite color is :red