新手询问如何在Python中退出while循环。

huangapple go评论61阅读模式
英文:

Newbie asking how to exit while loop in python

问题

我是新手学Python,没有编程经验。所以,我尝试使用函数来完成这种练习,尝试了不同的方法,但无法退出循环。所以,这个代码工作得很好,除了无法退出 while 循环。我不明白为什么 break 不起作用。

valinta = ()

def main():
    valinta = input("Enter input (Type 'lopeta' to quit): ")
    if len(valinta) <= 5:
       tulostus = "Default output"
    elif valinta == "lopeta":
        tulostus = "You quit"
    elif len(valinta) >= 6:
            tulostus = valinta

    printer(tulostus)

def printer(tulostus):
    print(tulostus)

while valinta != "lopeta":
    if valinta == "lopeta":
       break
    elif __name__ == "__main__":
        main()

我还尝试将 "lopeta" 更改为 0,但在所有变化中循环都开始了。

Enter input (Type 'lopeta' to quit): asdf
Default output
Enter input (Type 'lopeta' to quit): asfgghhj
asfgghhj
Enter input (Type 'lopeta' to quit): lopeta
You quit
Enter input (Type 'lopeta' to quit): 
英文:

I'm new to python, and don't really have experience with coding. So, i'm trying to do this kind of exercise using functions and I've tried different approaches but I just can't exit loop. So, this works just how it supposed to work, except it doesn't exit while - loop. And I don't understand why that break doesn't work

valinta = ()

def main():
    valinta = input(&quot;Anna sy&#246;te (Lopeta lopettaa): &quot;)
    if len(valinta) &lt;= (5):
       tulostus = (&quot;Oletustulostus&quot;)
    elif valinta == (&quot;lopeta&quot;):
        tulostus=(&quot;lopetit&quot;)
    elif len(valinta) &gt;= (6):
            tulostus = valinta

    tulostin(tulostus)

def tulostin(tulostus):
    print(tulostus)

while valinta !=(&quot;lopeta&quot;):
    if valinta == (&quot;lopeta&quot;):
       break
    elif __name__ == &quot;__main__&quot;:
        main()`

I also tried chancing "lopeta" to (0), but with all variations loop starts

Anna sy&#246;te (Lopeta lopettaa): asdf
Oletustulostus
Anna sy&#246;te (Lopeta lopettaa): asfgghhj
asfgghhj
Anna sy&#246;te (Lopeta lopettaa): lopeta
lopetit
Anna sy&#246;te (Lopeta lopettaa): 

答案1

得分: 1

以下是已翻译的代码部分:

def ask_input():
    valinta = input("Enter input (Type 'exit' to exit): ")
    if len(valinta) <= 5:
        tulostus = "Default output"
    elif valinta == "exit":
        tulostus = "You exited"
    elif len(valinta) >= 6:
        tulostus = valinta

    printer(tulostus)

    return tulostus

def printer(tulostus):
    print(tulostus)

def main():
    tulostus = ""
    while tulostus != "You exited":
        tulostus = ask_input()

if __name__ == "__main__":
    main()

输出是:

Enter input (Type 'exit' to exit): short
Default output
Enter input (Type 'exit' to exit): much longer than needed
much longer than needed
Enter input (Type 'exit' to exit): exit
You exited

请检查代码是否按预期运行,我认为它做了你要的事情,但我的芬兰语水平不是很好!

英文:

I suspect you want something like the following:

def ask_input():
    valinta = input(&quot;Anna sy&#246;te (Lopeta lopettaa): &quot;)
    if len(valinta) &lt;= (5):
        tulostus = &quot;Oletustulostus&quot;
    elif valinta == &quot;lopeta&quot;:
        tulostus = &quot;lopetit&quot;
    elif len(valinta) &gt;= 6:
        tulostus = valinta

    tulostin(tulostus)

    return tulostus

def tulostin(tulostus):
    print(tulostus)

def main():
    tulostus = &quot;&quot;
    while tulostus != &quot;lopetit&quot;:
        tulostus = ask_input()

if __name__ == &quot;__main__&quot;:
    main()

Output is:

Anna sy&#246;te (Lopeta lopettaa): short
Oletustulostus
Anna sy&#246;te (Lopeta lopettaa): much longer than needed
much longer than needed
Anna sy&#246;te (Lopeta lopettaa): lopeta
lopetit

See if you can see how it works as I think it does what you're looking for but my Finnish isn't great!

答案2

得分: 0

感谢您的评论。可以看到,我只是刚刚开始尝试学习编程,还没有走得很远。只是为了澄清,我最初为什么这样做:必须以某种方式声明“valinta”,而对于循环中的退出条件,这只是我尝试过的一种方法(但没有成功),因为我认为“while != ('lopeta')”只需要“if name....”,而主函数不会再次启动,直到条件匹配。

但是,我的问题是变量在错误的位置声明。我仍然有很多要学,但我想我必须从某个地方开始。

def ask_input():
    valinta = input("Enter input (Type 'Lopeta' to quit): ")
    if len(valinta) <= 5:
        tulostus = "Default Output"
    elif valinta == "Lopeta":
        tulostus = ()
        return None
    elif len(valinta) >= 6:
        tulostus = valinta
    tulostin(tulostus)
    return tulostus

def tulostin(tulostus):
    print(tulostus)

def main():
    tulostus = ""
    while tulostus != None:
        tulostus = ask_input()

if __name__ == "__main__":
    main()

所以,这就是我设法完成的方式。我遇到问题,因为“Lopeta”被打印出来,而对于赋值,它只需要退出循环。

只是为了明确一下,我曾经是一名医护人员,现在是一名工程学生,但不是信息技术专业的学生。我有机会参加在线Python入门课程,只是想尝试看看是否能够理解一些基本的编程。

英文:

Thanks for comments. As can be seen, I'm just starting to trying to learn coding, and I haven't got really far. Just to clarify, why I did it originally that way: had to declare 'valinta' somehow, and for exit condition in loop, it is just something I tried (but didn't work), because I thought that 'while != ("lopeta")' would only need 'if name....', and main wouldn't start again while condition wouldn't match.

But so, my problem were variable declared in wrong place. I still have lot to learn, but I guess I have to start somewhere

def ask_input():
valinta = input(&quot;Anna sy&#246;te (Lopeta lopettaa): &quot;)
if len(valinta) &lt;= (5):
    tulostus = &quot;Oletustulostus&quot;
elif valinta == &quot;Lopeta&quot;:
    tulostus = ()
    return None
elif len(valinta) &gt;= 6:
    tulostus = valinta
tulostin(tulostus)
return tulostus

def tulostin(tulostus):
print(tulostus)
def main():
tulostus = &quot;&quot;
while tulostus != None:
    tulostus = ask_input()
if __name__ == &quot;__main__&quot;:
main()

So, here is how I managed to do this. I had problems, because 'Lopeta' were printed, and for assignment it should just had to exit loop.

Just be be clear, I used to be paramedic and now I'm engineering student, but not for information technology. I have opportunity to take introduction to python online course,just wanted to try if I could figure out some basic coding.

huangapple
  • 本文由 发表于 2023年2月23日 22:52:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/75546466.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定