Python循环和程序重启,如果为true

时间:2015-06-27 00:46:17

标签: python loops while-loop loopingselector

请帮助我,因为我是一个真正的初学者,并希望了解更多。我想了解有关循环的更多信息,请查看此代码。

# 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"
    then restart?????
else answer = "no"
    print "Thanks for using my calculator!!!"

3 个答案:

答案 0 :(得分:1)

你可以尝试定义一个函数calc或你想要的东西并写下整个 该函数中的代码,只要你需要,你就可以直接调用该函数

def calc():
 your code

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

答案 1 :(得分:0)

<强>更新: 你必须使用这样的while循环:

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

答案 2 :(得分:0)

您还可以创建一个名为should_restart的变量(或任何您想要调用的变量),并在循环之前将其设置为True。如果should_restart等于True,则设置循环。然后你可以说如果用户输入是继续但如果用户回答否则设置should_restart等于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