如何解决在while循环中出现Python继续语句的问题
我正在学习如何在while循环中使用continue语句,并将此代码作为练习示例进行编写。我希望在打印最后一条消息之前得到一些“ undergrad”,“ grad”和“ phd”的回复。我可以继续执行此操作吗?
print("Welcome to Higher Education U. Please partake in the following roll call.")
name = input("What is your name? ")
level = input("Which program are you in: undergrad,grad,phd or other? ")
while True:
if level == 'undergrad':
print(f"Hi {name},you are one of the first undergrads.")
continue
elif level == 'grad':
print(f"Hi {name},you are one of the first grad students.")
continue
elif level == 'phd':
print(f"Hi {name},you are one of the first phd students.")
continue
else:
print(f"Hi {name},please consider applying to HEU!")
break
解决方法
当您到达缩进代码套件的末尾时,
if (!hash.Any(x => x.Contains(Helpers.GetRootUrl(externalUrl))))
循环会自动继续,因此您的while
套件无需手动执行。但是您需要将提示放在if
中,以便在您进行操作时不断提示输入更多数据。
您的代码可能是
while
对于多个值,您具有相同的算法,可以将它们放入容器中,并且仅实现一次该算法。您想跟踪申请人的数量,因此记录名称的字典很有意义。
print("Welcome to Higher Education U. Please partake in the following roll call.")
while True:
name = input("What is your name? ")
level = input("Which program are you in: undergrad,grad,phd or other? ")
if level == 'undergrad':
print(f"Hi {name},you are one of the first undergrads.")
elif level == 'grad':
print(f"Hi {name},you are one of the first grad students.")
elif level == 'phd':
print(f"Hi {name},you are one of the first phd students.")
else:
print(f"Hi {name},please consider applying to HEU!")
break
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。