Python quit()函数返回错误。我正在使用spyder 4.1.2

时间:2020-05-10 05:41:29

标签: python anaconda spyder

"Beginning of the Program"
print("Hi Sir and welcome to my calculator")
print("Please select from the below menu your math operation")

MenuOption = input("+ for Add, - for Subtract, * for Multiply, / for Division: ") 

if (MenuOption != "+" and MenuOption != "-" and MenuOption != "*" and MenuOption != "/"):
    print("You have typed wrong character, please try again")
    **# my problem is here, I want it to loop back to MenuOption  line**

else:
    print("Thanks for the correct selection")



FirstNumber = int(input("First Number: "))
SecondNumber = int(input("Second Number: "))

if (MenuOption == "+"):
    print(FirstNumber + SecondNumber)

elif (MenuOption == "-"):
    print(FirstNumber - SecondNumber)              

elif (MenuOption == "*"):
    print(FirstNumber * SecondNumber)

elif (MenuOption == "/"):
    print(FirstNumber / SecondNumber)

不应发生此错误。我很困惑我在这里做错了什么。我收到错误“未定义名称'quit'” 。哪个不应该来。

1 个答案:

答案 0 :(得分:1)

quit()exit()依赖于site模块,据我所知,它们被设计为以交互方式使用,而不是在实际程序或生产代码中使用。 / p>

相反,我建议使您的程序看起来像这样:

file = input("Enter file name:")
try:
    fhand = open(file,'r')
    count = 0
    for line in fhand:
        line =line.strip()
        if line.startswith('Subject'):
           count+=1
    fhand.close()
    print('There were,',count,'subject lines in ',file)
except FileNotFoundError:
    print("File not found")

您还可能希望立即读取文件并尽快将其关闭。