Python looping and program restart if true

久未见 提交于 2019-12-02 17:05:52

问题


Please help as I am a true beginner and would love to learn more. I am trying to learn more about looping so take a look at this code please.

# lets the user input first number
num1 = float(raw_input("Enter your first number  ---> "))

# lets the user input second number
num2 = float(raw_input("Enter your second number  ---> "))

#the next four lines sets the basic mathematical equations
addAns = num1+num2
subAns = num1-num2
mulAns = num1*num2
divAns = num1/num2

# ask the user to let the program know what equation to run with an 
option of running them all
operator = raw_input("What operator would you like to use(+,-,*,/ or ALL)")

#these if and else statements are based on the input by the user.         
Depending on the operator chosen it will print that equation.
if operator == "+":
    print "the result of:\t " ,num1, "+" ,num2, "=" ,addAns,
elif operator == "-":
    print "\nthe result of:\t " ,num1, "-" ,num2, "=" ,subAns,
elif operator == "*":
    print "\nthe result of:\t " ,num1, "*" ,num2, "=" ,mulAns,
elif operator == "/":
    print "\nthe result of:\t " ,num1, "/" ,num2, "=" ,divAns,
elif operator == "ALL" or "all" or "All":
    print "the result of:\t " ,num1, "+" ,num2, "=" ,addAns,
    print "\nthe result of:\t " ,num1, "-" ,num2, "=" ,subAns,
    print "\nthe result of:\t " ,num1, "*" ,num2, "=" ,mulAns,
    print "\nthe result of:\t " ,num1, "/" ,num2, "=" ,divAns,

the question is how do I get this program to start all over if I was to ask the user to tell me yes or no for example:

answer = raw_input("Would you like to try again?(yes or no)")
if answer = "yes"
    then restart?????
else answer = "no"
    print "Thanks for using my calculator!!!"

回答1:


you can try defining a function calc or what ever u want and write the whole code in that function and u can directly call that function whenever u need

def calc():
 your code

if answer == 'yes' or 'Yes':calc()
elif answer == 'no' or 'No':print "Thanks for using my calculator



回答2:


You could also make a variable called should_restart (or whatever you want to call it) and set it equal to True before the loop. Then set the loop to happen if should_restart is equal to True. Then you could say if the user inputs yes it continues but if the user answers no it sets should_restart equal to True

# decides whether to keep looping or not
should_restart = True
while should_restart == True:

    # lets the user input first number
    num1 = float(raw_input("Enter your first number  ---> "))

    # lets the user input second number
    num2 = float(raw_input("Enter your second number  ---> "))

    #the next four lines sets the basic mathematical equations
    addAns = num1+num2
    subAns = num1-num2
    mulAns = num1*num2
    divAns = num1/num2

    # ask the user to let the program know what equation to run with an 
    option of running them all
    operator = raw_input("What operator would you like to use(+,-,*,/ or ALL)")

    #these if and else statements are based on the input by the user.         
    Depending on the operator chosen it will print that equation.
    if operator == "+":
        print "the result of:\t " ,num1, "+" ,num2, "=" ,addAns,
    elif operator == "-":
        print "\nthe result of:\t " ,num1, "-" ,num2, "=" ,subAns,
    elif operator == "*":
        print "\nthe result of:\t " ,num1, "*" ,num2, "=" ,mulAns,
    elif operator == "/":
        print "\nthe result of:\t " ,num1, "/" ,num2, "=" ,divAns,
    elif operator == "ALL" or "all" or "All":
        print "the result of:\t " ,num1, "+" ,num2, "=" ,addAns,
        print "\nthe result of:\t " ,num1, "-" ,num2, "=" ,subAns,
        print "\nthe result of:\t " ,num1, "*" ,num2, "=" ,mulAns,
        print "\nthe result of:\t " ,num1, "/" ,num2, "=" ,divAns,
    answer = raw_input("Would you like to try again?(yes or no)")
    if answer == 'yes' or 'Yes':
        continue
    elif answer == 'no' or 'No':
        should_restart = False



回答3:


UPDATED: You have to use a while loop like this:

while True:
    # lets the user input first number
    num1 = float(raw_input("Enter your first number  ---> "))

    # lets the user input second number
    num2 = float(raw_input("Enter your second number  ---> "))

    #the next four lines sets the basic mathematical equations
    addAns = num1+num2
    subAns = num1-num2
    mulAns = num1*num2
    divAns = num1/num2

    # ask the user to let the program know what equation to run with an 
    #option of running them all
    operator = raw_input("What operator would you like to use(+,-,*,/ or ALL)")

    #these if and else statements are based on the input by the user.         
    #Depending on the operator chosen it will print that equation.
    if operator == "+":
        print "the result of:\t " ,num1, "+" ,num2, "=" ,addAns,
    elif operator == "-":
        print "\nthe result of:\t " ,num1, "-" ,num2, "=" ,subAns,
    elif operator == "*":
        print "\nthe result of:\t " ,num1, "*" ,num2, "=" ,mulAns,
    elif operator == "/":
        print "\nthe result of:\t " ,num1, "/" ,num2, "=" ,divAns,
    elif operator == "ALL" or "all" or "All":
        print "the result of:\t " ,num1, "+" ,num2, "=" ,addAns,
        print "\nthe result of:\t " ,num1, "-" ,num2, "=" ,subAns,
        print "\nthe result of:\t " ,num1, "*" ,num2, "=" ,mulAns,
        print "\nthe result of:\t " ,num1, "/" ,num2, "=" ,divAns,

    answer = raw_input("Would you like to try again?(yes or no)")
    if answer == 'no': break
print  "Thanks for using my calculator!!!"


来源:https://stackoverflow.com/questions/31084020/python-looping-and-program-restart-if-true

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!