在while循环中出现Python继续语句的问题

时间:2020-08-19 17:14:50

标签: python while-loop

我正在学习如何在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

1 个答案:

答案 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