python 暂停程序 等待用户输入 |
您所在的位置:网站首页 › python循环暂停 › python 暂停程序 等待用户输入 |
![]() 函数input()让程序暂停运行,等待用户输入文本。获取用户输入后,Python将其存储在一个变量中,以便使用 parrot.py message=input("Tell me something, and I will repeat it back to you: ") print(message) 编写清晰的程序greeter.py prompt="nIf you tell us who you are, we can personalize the messages you see." prompt+="nWhat is your first name?" name=input(prompt) print("nHello, "+name.title()+"!")创建多行字符串: 第1行将消息的前半部分存储在变量prompt中; 在第2行中,运算符+=在存储在prompt中的字符串末尾附加一个字符串 使用int()来获取数值输入使用函数input()时,Python将用户输入解读为字符串 将输入用于数值比较时,Python会引发错误,因为它无法将字符串和整数进行比较:不能将存储在age中的字符串'21'与数值18进行比较 age = input("How old are you? ") age = int(age) print(age >= 18)rollercoaster.py 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.") 求模运算符parrot.py 求模运算符(%)是一个很有用的工具,它将两个数相除并返回余数。 #判断一个数是奇数还是偶数 number=input("Enter a number, and I'll tell you if it's even or odd: ") number=int(number) if number%2==0: print("nThe number "+str(number)+" is even.") else: print("nThe number "+str(number)+" is odd.")==练习== #7-1 汽车租赁:编写一个程序,询问用户要租赁什么样的汽车,并打印一条消息,如“Let me see if I can find you a S |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |