python之用户输入和while循环

用户输入和while循环

  1. 函数input()的工作原理
    函数input()让程序暂停运行,等待用户输入一些文本。获取输入后,python将其存储在一个变量中使用。
    举例体会:
message = input("tell me somthing, and i will repeat it back to you: ")
print(message)
tell me somthing, and i will repeat it back to you: aa
aa

每当使用input()函数时,应该清晰的提示用户下一步该如何操作。

name =  input("PLease enteryour name: ")
print("\nHello, " + name + "!")
PLease enteryour name: aa

Hello, aa!
prompt = "if you tell us who you are, wo can personalize the message you see."
prompt += "\nwhat is your first name?"
name  = input(prompt)

print("\nHello, " + name + "!")
if you tell us who you are, wo can personalize the message you see.
what is your first name?qq

Hello, qq!

使用int()来获取数值的输入:
使用函数input()时,python将用户输入解读为字符串,比如:

>>> age = input("how old are you:")
how old are you:21
>>> age
'21'

输入的是数字21,但是打印age变量的值时,返回的确实‘21’,用字符串表示输入的数字,刚才是怎么判断数字以字符串的形式显示的呢,就是数字21两边有单引号。
所以如果在python中想直接使用用户属于的数字,做某些判断或者检查是不行的,需要把字符串数字转换为数字以后,才使用,比如:

age = input("how old are you? ")
age = int(age)
if age>= 30:
	print(age)
else:
	print("age too small!")
how old are you? 55
55
how old are you? 21
age too small!
height = input("how tall are you, in inches?")
height = int(height)

if height >= 36:
	print("\nyou're tall enough to ride!")
else:
	print("\nyou'll be able to ride when you're a little older.--command")
how tall are you, in inches?34

you'll be able to ride when you're a little older.--command

how tall are you, in inches?66

you're tall enough to ride!

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

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

number = input("enter a number, and I'll tell you if it's even or odd: ")
number = int(number)

if number % 2 == 0:
	print("\n the number " + str(number) + " is even.")
else:
	print("\n the number " + str(number) + " is odd.")
enter a number, and I'll tell you if it's even or odd: 5

 the number 5 is odd.
enter a number, and I'll tell you if it's even or odd: 6

 the number 6 is even.

注:如果使用的是python 2.7 version,应使用函数raw_input()来提示用户输入。这个函数与python3中的input()一样,也将输入解读为字符串。
python 2.7也包含函数input(),但它将用户输入解读为python代码,并尝试运行它们,如果运行它们最好的执行结果是出错,但有可能结果会是执行成功,但是得不到自己的预期结果。所以在python2.7中,请使用raw_input()而不是input()来获取输入。

car = input("please tall me, your select car: ")
print("let me see if I can find you a " + car)
please tall me, your select car: bwa
let me see if I can find you a bwa

peopel = input("please tel me, your total many peopel eatting: ")
peopel = int(peopel)

if peopel > 8:
	print("sorry, current table is full!")
else:
	print("ok, have empty table.please...")
please tel me, your total many peopel eatting: 9
sorry, current table is full!

please tel me, your total many peopel eatting: 5
ok, have empty table.please...

num = input("please input int number: ")

num = int(num)

if num % 10 != 0:
	print("the num: " + str(num) + " , not 10 Whole multiple")
else:
	print("yes: " + str(num) + " is 10 Whole multiple")
please input int number: 5
the num: 5 , not 10 Whole multiple

please input int number: 10
yes: 10 is 10 Whole multiple

  1. While循环
    for循环用于针对集合中的每一个元素的一个代码块,而while循环不断的运行,直到指定的条件不满足为止。
    在我们使用的很多程序中都有可能包含while循环。例如,游戏使用while循环,确保在玩家想玩时不断运行,并在玩家想退出时停止运行。如果程序在用户没有让它停止时停止,或者在用户要退时还在继续运行,那就太没有意思了。
    使用while循环让程序在用户意愿时不断运行,比如:
prompt = "\ntell me somthing, and i will repeat it back to you: "
prompt += "\nenter 'quit' to end the program."
message = ""
while message != 'quit':	
	message = input(prompt)
	print(message)
tell me somthing, and i will repeat it back to you: 
enter 'quit' to end the program.go
go

tell me somthing, and i will repeat it back to you: 
enter 'quit' to end the program.quit
quit


------------------
(program exited with code: 0)
Press return to continue

当然,我们还可以使用标记(flag)来决定是否要继续运行:
在要求很多条件都满足才继续运行的程序中,可定义一个变量,用于判断整个程序是否处于活动状态,这个变量被称为标志,充当了程序的交通信号灯,比如:

prompt = "\ntell me somthing, and i will repeat it back to you: "
prompt += "\nenter 'quit' to end the program."
message = ""

active = True
while active:
	message = input(prompt)
	if message == 'quit':
		active = False
	else:
		print(message)

tell me somthing, and i will repeat it back to you: 
enter 'quit' to end the program.go
go

tell me somthing, and i will repeat it back to you: 
enter 'quit' to end the program.do
do

tell me somthing, and i will repeat it back to you: 
enter 'quit' to end the program.quit


------------------
(program exited with code: 0)
Press return to continue

使用break退出循环,例如:

prompt = "\nplease enter the name of a city you have visited: "
prompt += "\n(enter 'quit' when you are finished)"

while True:
	city = input(prompt)
	
	if city == 'quit':
		break
	else:
		print("i'd love to go to " + city.title() + "!")


please enter the name of a city you have visited: 
(enter 'quit' when you are finished)go
i'd love to go to Go!

please enter the name of a city you have visited: 
(enter 'quit' when you are finished)go
i'd love to go to Go!

please enter the name of a city you have visited: 
(enter 'quit' when you are finished)quit


------------------
(program exited with code: 0)
Press return to continue

在循环中使用continue,continue语句不像break语句一样,直接退出程序,而是忽略本次的循环,而是直接跳到开头进行下一次的循环。

current_number = 0

while current_number < 10:
	current_number += 1
	if current_number % 2 == 0:
		continue
		
	print(current_number)
1
3
5
7
9


------------------
(program exited with code: 0)
Press return to continue

如果余数不为0就打印出来,如果余数为0,就执行下次循环。
注意:在编写循环程序时,一定更要避免死循环,那样程序就会无限的执行下去,除非时有需要这样的场景时,才是用无限循环,否则,在循环中,一定要有退出循环的条件。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值