首页 > 解决方案 > 为什么 Python 不接受 print 在 While 循环之外?

问题描述

#Tristan Ledlie
#CSC1194C1
#10/21/2020

#Initialize variables
first = True
enter = ""

#While the user hasn't ended the program
while (enter != "END"):
    #Take input for enter
    enter = float(input("Please input a value(Or END to end the program)... "))
    #If this is the first loop:
    if (first == True):
        minimum = enter
        first = False

    #If enter is lower than the minimum number:
    elif (enter < minimum):
        minimum = enter

    else ():

print(minimum)

i = input("Press Enter to close the window...")

解释器一直说它期望在打印之前缩进。我不知道出了什么问题。对我来说完全是无稽之谈。

标签: python-3.xsyntax-error

解决方案


你在 else 之后缺少一个语句,如果你不想发生任何事情,请使用pass.

while condition:
    # ...your code...
    if condition:
        # ...your code...
    elif condition:
        # ...your code...
    else:
        pass

print(statement)

另外,后面没有括号else


推荐阅读