input()函数
input()让程序暂停使用,等待用户输入一些文本。
获取用户输入后,Python将其存储在一个变量中,以方便后续使用
message = input("Tell me something, and I will repeat it back to you: ") print(message)
输出
Tell me something, and I will repeat it back to you: Hello everyone!
Hello everyone!
height = input("How tall are you, in inches? ") height = int(height)#int()用于将获取的输入转换为整型数格式 if height >= 36: print("\nYou‘re tall enough to ride! ") else: print("\nYou‘ll be able to ride when you‘re a little older. ")
while循环
number = 1 while number <= 5: print(number) number += 1
遇到break,立即跳出循环
遇到continue,结束本次循环,进入下一次循环
使用用户输入来填充字典
responses = {}
设置一个标志位,指出调查是否继续
polling_active = True while polling_active: name = input("\nWhat is your name? ") response = input("Which mountain would you like to climb someday? ") responses[name] = response repeat = input("Would you like to let another person respond?(yes/no)") if repeat =="no": polling_active = False print("\n--- Poll Result ---") for name, response in responses.items(): print(name.title() + "would like to climb" + response + ".")
原文地址:https://www.cnblogs.com/dingdangsunny/p/12250319.html
时间: 2024-11-10 17:10:22