TeresaMalteasar
TeresaMalteasar

Reputation: 23

Trying to offer user the chance to restart a program by calling a function but it doesn't work

I'm working on a little python calculator and once it has run, I want to ask the user if they want to start again, and then restart the program based on their response.

I have this:

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!")

But this is the output (from the question asking user whether they want to start again):

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

I'm VERY new to coding so I appreciate this might seem like a very trivial question :)... but I appreciate any help here!

Thanks so much!

Upvotes: 2

Views: 113

Answers (2)

Shaiq Kar
Shaiq Kar

Reputation: 13

You can try like this

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()

Upvotes: -1

ckunder
ckunder

Reputation: 2300

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!")

The problem is you are assigning print(input(...)) to restart, which is NoneType.

Upvotes: 3

Related Questions