Python语法错误,“break”在循环之外

2024-06-02 08:20:02 发布

您现在位置:Python中文网/ 问答频道 /正文

try:

    ip = float(input("Enter a number to find it's absolute value (press 'e' to exit):"))
    if ip == 'e':
        break

Python说break不在循环中。有人知道怎么解决这个问题吗?提前谢谢。


Tags: toipnumberinputifvalueexitit
2条回答

break只能用于中断for循环或while循环。如果在其他地方使用,则会抛出错误。

以下是^{}的概述:

如果要从函数中出来,需要使用return。或者,如果要退出程序,请执行以下操作:

raise SystemExit

break不能在循环外使用。

如果您在一个函数中,请使用return,否则使用sys.exit()

相关问题 更多 >