【发布时间】:2023-08-29 20:05:01
【问题描述】:
Python 版本=3.5
-
所以我想知道如何根据用户的输入设置变量。 例如,如果用户要对此回答
7:residents=input("你家住了多少人?")
EDIT= 如果他们输入了 7- 我如何询问每个人的姓名??
谢谢!
【问题讨论】:
Python 版本=3.5
所以我想知道如何根据用户的输入设置变量。
例如,如果用户要对此回答 7:
residents=input("你家住了多少人?")
EDIT= 如果他们输入了 7- 我如何询问每个人的姓名??
谢谢!
【问题讨论】:
def get_int(prompt):
while True:
try:
return int(input(prompt))
except ValueError: # not an int!
pass # try again
residents = get_int("How many people live at your house? ")
编辑:您可以使用列表,而不是为每个人命名变量:
resident_names = [input("Name of resident {}: ".format(i)) for i in range(1, residents + 1)]
【讨论】: