《Python编程从入门到实践》_第七章_用户输入和whlie循环

函数input()的工作原理

函数input()让程序暂停运行,等待用户输入一些文本。获取用户输入后,python将其存储在一个变量中,以方便你使用。

#输入用户名
username = input("Please input your username:")
print (username)
#运行结果
Please input your username:Frank
Frank

变量传递给函数input()

有的时候,我们需要提示的字符串比较长,可以将提示的语句放在一个变量里面。

#greeter
prompt = "If you tell us who you are,we can personalize the messages you see."
prompt += "
What is your first name?"
name = input(prompt)
print ("
hello, " + name + "!") 
#运行结果
If you tell us who you are,we can personalize the messages you see.
What is your first name?Frank
hello, Frank!

使用int()来获取数值输入

我们input()所得到的是字符串数据,包括你想输入的整型是123,但是保存到变量里面的时候却是字符串"123"。

#判断年龄是否达到做过山车的年龄
age = input("How old are you?")
if age >=18:
    print("You can ride!")
else:
    print("You can't ride ")
#运行结果,当我们不使用int()把字符串转为整型的话,age是不能和数值18比较的
TypeError: '>=' not supported between instances of 'str' and 'int'

 所以需要使用函数int()

#判断年龄是否达到做过山车的年龄
age = input("How old are you?")
if int(age) >=18:
    print("You can ride!")
else:
    print("You can't ride ")
#运行结果
How old are you?18
You can ride!

求模运算符

 处理数值信息时,求模运算符(%)是一个很有用的工具,它将两个数相除并返回余数:

>>> 4 % 3
1
>>> 5 % 3
2
>>> 6 % 3
0
>>> 7 % 2
1

可以用来判断奇偶数。

python2中的raw_input()

在python2中使用raw_iput()来提示用户输入,这个与python3中的input()是一样的,也将输入解读为字符串。python2中也存在input(),但它将用户输入解读为python代码,并尝试执行它。

username = raw_input("Please input your username:")
print (username)
username = input("Please input your username:")
print (username)
#运行结果
Please input your username:cisco
cisco
Please input your username:cisco
Traceback (most recent call last):
  File "C:Python27	est.py", line 3, in <module>
    username = input("Please input your username:")
  File "<string>", line 1, in <module>
NameError: name 'cisco' is not defined

 我们会看到在python2中使用raw_input可以正常输出,但是使用input就不能正常输出的,因为他把你输入的当做可执行的代码。

 while循环

while循环不断地运行,直到指定的条件不满足为止。

#输出1-5
current_number = 1
while current_number <= 5:
    print(current_number)
    current_number+=1
#运行结果
1
2
3
4
5

使用标志

有的时候使用标志可以简化while的语句,因为不需要在其中做任何比较--相关的逻辑由程序的其他部分处理。

#quit退出
prompt = "
Tell me something,and i will repeat it back to you:"
prompt += "
Enter 'quit' to end the program. "
active = True
while active:
    message = input(prompt)
    if message == 'quit':
        active = False
    else:
        print(message)
#运行结果
Tell me something,and i will repeat it back to you:
Enter 'quit' to end the program. hello!
hello!
Tell me something,and i will repeat it back to you:
Enter 'quit' to end the program. my name is Frank
my name is Frank
Tell me something,and i will repeat it back to you:
Enter 'quit' to end the program. quit

break和continue

break:会结束本层循环,不再运行循环中余下的代码;
continue:结束本次循环,仅仅结束一次循环,不再运行本次余下的代码。
#break举例
current_number = 1
while current_number < 10:
    if current_number == 5:
        current_number+=1
        break
    else:
        print(current_number)
        current_number+=1
#运行结果 
1
2
3
4
#continue举例
current_number = 1
while current_number < 10:
    if current_number == 5:
        current_number+=1
        continue
    else:
        print(current_number)
        current_number+=1
#运行结果
1
2
3
4
6
7
8
9
原文地址:https://www.cnblogs.com/liubinsh/p/7010703.html