【问题标题】:Python looping and program restart if true如果为 true,则 Python 循环和程序重新启动
【发布时间】:2015-06-27 00:46:16
【问题描述】:

请帮忙,因为我是一个真正的初学者,很想了解更多。我正在尝试了解有关循环的更多信息,因此请查看此代码。

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

【问题讨论】:

  • 带循环。查看official Python tutorial
  • 将您的代码包装在一个函数中。这是编码的下一个“步骤”。第 1 步 - 了解变量/数据类型,第 2 步 - 了解如何创建行为(函数、类、方法、模块、库等)。然后你就可以开始构建灵活的程序了。祝你好运。
  • 谢谢大家。我会做更多的研究。

标签: python loops while-loop loopingselector


【解决方案1】:

你可以尝试定义一个函数 calc 或者你想要的任何东西并编写整个 该函数中的代码,您可以在需要时直接调用该函数

def calc():
 your code

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

【讨论】:

    【解决方案2】:

    您还可以创建一个名为 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
    

    【讨论】:

    • 非常感谢您的帮助,即使回答“否”,这仍然让我回到最开始。
    【解决方案3】:

    更新: 您必须像这样使用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!!!"
    

    【讨论】:

    • @PaulHofer 哦,好的,抱歉,我犯了一个小错误。我更新了我的答案,现在可以了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-04
    • 2022-01-13
    • 2017-10-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多