我正在学习如何在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
答案 0 :(得分:0)
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