试图通过调用函数为用户提供重新启动程序的机会,但不起作用

2024-03-28 21:36:54 发布

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

我正在开发一个小型python计算器,一旦它运行了,我想询问用户是否希望再次启动,然后根据他们的响应重新启动程序

我有这个:

def begin():
    print("WELCOME TO THE PYTHON CALCULATOR\n\nYOUR SELECTIONS:\nBasic (+, -, *, /)\nAdvanced (power, square root)\nBMI\nMortgage\nTrip\n")
    selection = input("What type of Calculator would you like to use? (NB: Case sensitive): ")

    if selection == "Basic":
        basic_input()
    if selection == "Advanced":
        adv_input()
    if selection == "BMI":
        imp_or_met = input("Do you prefer to use metric or imperial units? (Metric/Imperial): ")
        if imp_or_met == "Metric":
            bmi_met()
        elif imp_or_met == "Imperial":
            bmi_imp()
    if selection == "Mortgage":
        mort_calc()
    if selection == "Trip":
        trip_calc()

begin()
#restart calculator
restart = print(input("Would you like to use the calculator again?\n1: Yes, 2: No\n"))
if restart == "1":
    begin()
else:
    print("Thank you for using the calculator!")

但这是输出(来自询问用户是否要重新开始的问题):

Would you like to use the calculator again?
1: Yes, 2: No
1
Thank you for using the calculator!

我对编码非常陌生,所以我理解这可能看起来是一个非常琐碎的问题:)。。。但我很感激这里的任何帮助

非常感谢


Tags: orthetoyouinputifusecalculator
2条回答

你可以这样试试

def begin():
    #code for your begin() function
    #by adding restart code here you can restart the function as many times as you want
    restart = int(input("Would you like to use the calculator again?\n1: Yes, 2: No\n"))

    if restart == 1:
        begin()
    else:
        print("Thank you for using the calculator!")
        SystemExit(0) #To exit from the program

if __name__ == '__main__':
   begin()
restart = input("Would you like to use the calculator again?\n1: Yes, 2: No\n")

if restart == "1":
    begin()
else:
    print("Thank you for using the calculator!")

问题是您正在将print(input(...))分配给restart,即NoneType

相关问题 更多 >