我需要在Python中执行以下操作:向用户询问其昵称。如果他输入的昵称以“ A”开头,则全部设置。如果昵称以另一个字母开头,我需要继续询问有效的昵称。我创建了一些代码,但是如何循环第二部分?谢谢。
question_name = "What is your name?"
print(question_name)
answer = input()
your_name = answer
while your_name.startswith("A"):
print(your_name.startswith("A"))
print("The name starts with A")
break
else:
print("Please enter the valid name")
print(question_name)
answer = input()
您应该反转while条件。应该是这样的:
question_name = "What is your name?"
print(question_name)
answer = input()
your_name = answer
while not your_name.startswith("A"):
print("Please enter the valid name")
print(question_name)
answer = input()
your_name = answer
print(your_name.startswith("A"))
print("The name starts with A")
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句