gpt4 book ai didi

python - 新手在 Python 中遇到简单的 "while"重复

转载 作者:行者123 更新时间:2023-11-28 18:51:04 25 4
gpt4 key购买 nike

程序的重点是询问用户的姓名(首字母自动大写)。

然后它会询问年龄和性别。如果 age 超过 130 或为负数,则会抛出错误

程序应该打印出所有信息,但我无法弄清楚 while 循环条件。谁能帮我弄清楚 while 循环条件?

-edit- 虽然 Pastebin 的链接被删除了,但我认为那里有重要的信息。所以,我仍然会给你链接: http://pastebin.com/UBbXDGSt

name = input("What's your name? ").capitalize()
age = int(input("How old are you "))
gender = input("From what gender are you? ").capitalize()

while #I guess I should write something behind the "while" function. But what?
if age >= 130:
print("It's impossible that you're that old. Please try again!")
elif age <= 0:
print('''It should be logical that ages are written in positive numbers! Well, try again! =)''')

age = int(input("How old are you? "))

print("Your name is ",name, ". You are ", age, "years old." "\nYou are ", gender, ".")

最佳答案

如果有有效输入,您可以设置一个关闭/打开的标志。这将解决您的 while 循环

的问题
 name = input("What's your name? ").capitalize()
gender = input("From what gender are you? ").capitalize()
ok = False #flag
while not ok:
age = int(input("How old are you "))
if age >= 130:
print("It's impossible that you're that old. Please try again!")
elif age <= 0:
print('''It should be logical that ages are written in positive numbers! Well, try again! =)''')
else:
ok = True
print("Your name is ",name, ". You are ", age, "years old." "\nYou are ", gender, ".")

关于python - 新手在 Python 中遇到简单的 "while"重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13105558/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com