英文:
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("Anna syöte (Lopeta lopettaa): ")
if len(valinta) <= (5):
tulostus = ("Oletustulostus")
elif valinta == ("lopeta"):
tulostus=("lopetit")
elif len(valinta) >= (6):
tulostus = valinta
tulostin(tulostus)
def tulostin(tulostus):
print(tulostus)
while valinta !=("lopeta"):
if valinta == ("lopeta"):
break
elif __name__ == "__main__":
main()`
I also tried chancing "lopeta" to (0), but with all variations loop starts
Anna syöte (Lopeta lopettaa): asdf
Oletustulostus
Anna syöte (Lopeta lopettaa): asfgghhj
asfgghhj
Anna syöte (Lopeta lopettaa): lopeta
lopetit
Anna syö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("Anna syöte (Lopeta lopettaa): ")
if len(valinta) <= (5):
tulostus = "Oletustulostus"
elif valinta == "lopeta":
tulostus = "lopetit"
elif len(valinta) >= 6:
tulostus = valinta
tulostin(tulostus)
return tulostus
def tulostin(tulostus):
print(tulostus)
def main():
tulostus = ""
while tulostus != "lopetit":
tulostus = ask_input()
if __name__ == "__main__":
main()
Output is:
Anna syöte (Lopeta lopettaa): short
Oletustulostus
Anna syöte (Lopeta lopettaa): much longer than needed
much longer than needed
Anna syö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("Anna syöte (Lopeta lopettaa): ")
if len(valinta) <= (5):
tulostus = "Oletustulostus"
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()
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论