简体   繁体   English

初学者:Python--如何将 elif 语句嵌入到 while 循环中(每次通过循环都会改变 output 的路线?!)

[英]BEGINNER: Python--How to embed elif statements into a while loop (that changes the route of output each time through the loop?!)

##Here's an example of what I've been working on: ##so far I've used num set to zero and num set to the full num=int(input statement... I've learned both ways so far and I'm not sure if I need to use it at all, but when I do it's usually for when I have a for statement asking to keep track of for x in range (0, inp+1): which I've already tried to no avail... ##这里是我一直在做的一个例子:##到目前为止,我已经使用了 num 设置为零和 num 设置为完整的 num=int(input 语句......到目前为止,我已经学会了两种方法我不确定我是否需要使用它,但是当我这样做时,通常是因为我有一个 for 语句要求跟踪范围内的 for x (0, inp+1): 我已经尝试过无济于事...

##okay, so here's what I've been trying to do for THREE days now... and it will drive me crazy till I get this; ##好的,这就是我三天以来一直在尝试做的事情......在我得到这个之前,它会让我发疯; I'm trying to get the program to give output while true (that num is not equal to 11, if not equal to 11; print "nope", if nope isn't printed, it's supposed to go on to the next statement. etc,. etc,. until it hits 11. But here's the thing, It's SUPPOSED to try the first statement while true: and change each time through the loop! I have a bunch of other ways I've tried this while racking my brain over it and haven't gotten it QUITE right yet so here's the basic nitty-gritty:我正在尝试让程序在为真时提供 output(如果不等于 11,则该 num 不等于 11;打印“nope”,如果未打印 nope,则应该在下一个语句中为 go。等等,等等,直到它达到 11。但事情是这样的,它应该尝试第一个语句,而它是真的:并且每次都通过循环改变!我有很多其他的方法,我在绞尽脑汁的同时尝试了这个在它上面,还没有完全正确,所以这里是基本的细节:

num=0

while num != 11:
    num=int(input("Please enter a number from 1 to 20 (17 to stop): "))
    print(num)
    if num != 11:
        print("nope")
    elif num >=3:
        print("history")
    elif num < 20:
        print("bye")
    elif num > 12:
        print("cat")
    elif num >= 7:
        print("push")
    else:
        print("sat")```

The reason it's not working is because any number that isn't 11 will be caught in the if, then the loop starts again.它不工作的原因是任何不是 11 的数字都会被 if 捕获,然后循环再次开始。 If you type 11 the loop just prints history then exits.如果您键入 11,则循环仅打印历史记录然后退出。 Doing this will first check if it's not 11, then check the other conditions.这样做将首先检查它是否不是 11,然后检查其他条件。 It's a bit unclear what you want to happen.有点不清楚你想要发生什么。 And 11 is stopping not 17. But hopefully this helps a bit. 11 停止而不是 17。但希望这会有所帮助。

num=0
while num != 11:
    num=int(input("Please enter a number from 1 to 20 (17 to stop): "))
    print(num)
    if num != 11:
        print("nope")
    if num >=3:
        print("history")
    elif num < 20:
        print("bye")
    elif num > 12:
        print("cat")
    elif num >= 7:
        print("push")
    else:
        print("sat")

if I enter 8, it's supposed to tell me "nope";如果我输入 8,它应该告诉我“不”; next if I enter 19, it's supposed to just show me the printed input "19";接下来如果我输入 19,它应该只显示打印的输入“19”; then if I want to enter 11, it's supposed to just put "11" as the output rather than any of the words.那么如果我想输入 11,它应该只是把“11”作为 output 而不是任何单词。 Once it's finished with the loop it starts all over again;一旦完成循环,它就会重新开始; in the second round it's like entering "3" and I'm somehow supposed to get "nope"在第二轮中,就像输入“3”一样,我应该以某种方式得到“不”

It sounds like you just want something like:听起来你只想要这样的东西:

while True:
    num = int(input("Please enter a number from 1 to 20 (17 to stop): "))
    print(num)
    if num == 11:
        continue
    print("nope")
    while num != 11:
        num = int(input("Please enter a number from 1 to 20 (17 to stop): "))
        print(num)

From your description it doesn't sound like you're ever supposed to see any of the other words so all of that code is unnecessary.根据您的描述,听起来您不应该看到任何其他单词,因此所有这些代码都是不必要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM