前言
今天是学习python的第九天,这次是接着input的使用的,话不多说,直接上干货。
用户输入
int函数的实际应用
判断一个人是否满足做过山车的要求
height = input("How tall are you,in inches?")
height = int(height)#这里相当于c语言中的强制转换
if height >= 48:
print("\nYou are tall enough to ride")
else:
print("\nYou'll be able to ride when you're a little older.")
结果:
How tall are you,in inches?40
You’ll be able to ride when you’re a little older.
(插入)求模运算符(%)用于求余数
>>> 4%3
1
>>> 5%3
2
>>> 6%3
0
>>> 7%3
1
判断奇偶数
number = input("Enter the number,and I will tell you if it's even or odd:")
number = int(number)
if number % 2 == 0:
print(f"The number is even")
elif number % 2 >0:
print(f"This number is odd")
结果:
Enter the number,and I will tell you if it’s even or odd:56
The number is even
餐馆预定座位
information = input("Hello sir,How many of you?:")
information = int(information)
if information > 8:
print(f"There are no free desks,sorry")
else:
print(f"welcome to there,and Here are your desks")
结果:
Hello sir,How many of you?:9
There are no free desks,sorry
10的整倍数
number = input("Please input a number,and I will tell you 他是不是10的倍数:")
number = int(number)
if number % 10 == 0 :
print(f"这个数是10的倍数")
elif number % 10> 0 :
print(f"这个数不是10的倍数")
结果:
Please input a number,and I will tell you 他是不是10的倍数:50
这个数是10的倍数
while循环
while会不断运行代码块,直至所指定的条件不满足为止
使用while循环
current_number = 1
while current_number <=5:#这里使用了while语句,所指定的条件是当current_number小于等于5就停止循环
print(current_number)#每循环一次就打印一次current_number
current_number +=1#每循环一次就变量进行加一
结果:
1
2
3
4
5
让用户选择何时退出
可以使用while循环让程序在用户想退出就退出
prompt = "\n Tell me something,and I wiil repeat it back to you:"#这里说明了该程序的功能,就是打印用户所打印出来的语句
prompt += "\nEnter'quit' to end the program."#这里使用了‘+’使得变量没有改变
message = ""#创建一个空语句,让用户输入
while message!= 'quit':#使用while语句,结束的条件是message = ‘quit’
message = input(prompt)#没循环一次用户就可以多输入一次
print(message)#打印用户所输入的
结果:
Tell me something,and I wiil repeat it back to you:
Enter’quit’ to end the program.hello
hello
Tell me something,and I wiil repeat it back to you:
Enter’quit’ to end the program.ni zai gan ma ne?
ni zai ganma ne?
Tell me something,and I wiil repeat it back to you:
Enter’quit’ to end the program.quit
quit
Process finished with exit code 0
但是该程序有一点不行,就是当我们输入quit来结束程序的时候 quit也会被打印出来这个时候需要使用到if语句。修改如下
prompt = "\n Tell me something,and I wiil repeat it back to you:"
prompt += "\nEnter'quit' to end the program."
message = ""
while message!= 'quit':
message = input(prompt)
if message != 'quit':
print(message)
结果:
Tell me something,and I wiil repeat it back to you:
Enter’quit’ to end the program.quit(我输入的)
Process finished with exit code 0
使用标志
在要求很多条件都满足才能继续运行的程序中,可定义一个变量,用于判断整个程序是否处于活动状态,这个变量称为标志。当这个变量为ture时程序可以继续运行下去,而当其中有一个条件使得这个变量为false时程序就不能运行。
prompt = "\n Tell me something,and I wiil repeat it back to you:"
prompt += "\nEnter'quit' to end the program."
active = True#标志,先使标志为True
while active:
message = input(prompt)
if message == 'quit':#判断条件,假如输入了quit那么这个标志变脸就会变成false从而程序无法运行
active = False
else:
print(message)
结果:
Tell me something,and I wiil repeat it back to you:
Enter’quit’ to end the program.离散数学
离散数学
Tell me something,and I wiil repeat it back to you:
Enter’quit’ to end the program.给老子爬
给老子爬
Tell me something,and I wiil repeat it back to you:
Enter’quit’ to end the program.quit
Process finished with exit code 0
今天就学到这里吧,有点短但是容易消化,加油。