Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.0+;:试图通过IF语句重新启动一段代码_Python_Loops_Scripting - Fatal编程技术网

Python 3.0+;:试图通过IF语句重新启动一段代码

Python 3.0+;:试图通过IF语句重新启动一段代码,python,loops,scripting,Python,Loops,Scripting,所以我尝试创建一个脚本,用户可以在其中输入一个值(现在是1、2或3),它提供了一个数学函数,可以用来找到答案。但是我想问用户是想重新启动这个数学函数,还是返回菜单要求用户选择一个不同的函数 我只学了几周python,所以我真的不知道怎么做,如果有任何帮助,我将不胜感激。谢谢 这是我的密码: choice = int(input("What program would you like to run? \n")) #chooses one of the programs recarea = 1

所以我尝试创建一个脚本,用户可以在其中输入一个值(现在是1、2或3),它提供了一个数学函数,可以用来找到答案。但是我想问用户是想重新启动这个数学函数,还是返回菜单要求用户选择一个不同的函数

我只学了几周python,所以我真的不知道怎么做,如果有任何帮助,我将不胜感激。谢谢

这是我的密码:

choice = int(input("What program would you like to run? \n"))

#chooses one of the programs
recarea = 1
circarea = 2
fah = 3

if choice == 1:

    print("This program calculates the area and perimeter of a rectangle:")

    length = float(input("Type in the length: \n"))
    width = float(input("Type in the width: \n"))
    area = length * width
    perimeter = (2*length)+(2*width)

    print("The area of the rectangle is ","{0:.2f}".format(area),"cm and the perimeter is ","{0:.2f}".format(perimeter),"cm",'\n\n')

    reuse = str(input("Would you like to restart the script ?"))

    if reuse == "Yes" or reuse == "yes" or reuse == "y" or reuse == "Y":
        print("Restarting Script...\n")

    else:
        reuse == "No" or reuse == "no" or reuse == "n" or reuse == "N"
        print("Returning to the menu...\n\n")

您需要将代码分解为函数:

def main():
    print("What program would you like to run? \n")
    choice = int(input("1 - rectangle area\n2 - circle area\n3 - fah"))
    if choice == 1:
        print("This program calculates the area and perimeter of a rectangle:")
        length = float(input("Type in the length: \n"))
        width = float(input("Type in the width: \n"))
        area = length * width
        perimeter = (2*length)+(2*width)
        print("The area of the rectangle is ","{0:.2f}".format(area),"cm and the perimeter is ","{0:.2f}".format(perimeter),"cm",'\n\n')
    elif choice == 2:
        print("Not implemented.")
    elif choice == 3:
        print("Not implemented.")
    else:
        print("I didn't understand that input please type 1, 2 or 3.")
        main()
    askRestart()

def askRestart():
    reuse = str(input("Type 'Y' to restart or 'X' to exit()."))
    if reuse.lower() == 'y':
        main()
    elif reuse.lower() == 'x':
        pass
    else:
        print("I didn't understand that input please type 'Y' or 'X'")
        askRestart()
然后调用函数运行程序:

main()

您需要将代码分解为函数:

def main():
    print("What program would you like to run? \n")
    choice = int(input("1 - rectangle area\n2 - circle area\n3 - fah"))
    if choice == 1:
        print("This program calculates the area and perimeter of a rectangle:")
        length = float(input("Type in the length: \n"))
        width = float(input("Type in the width: \n"))
        area = length * width
        perimeter = (2*length)+(2*width)
        print("The area of the rectangle is ","{0:.2f}".format(area),"cm and the perimeter is ","{0:.2f}".format(perimeter),"cm",'\n\n')
    elif choice == 2:
        print("Not implemented.")
    elif choice == 3:
        print("Not implemented.")
    else:
        print("I didn't understand that input please type 1, 2 or 3.")
        main()
    askRestart()

def askRestart():
    reuse = str(input("Type 'Y' to restart or 'X' to exit()."))
    if reuse.lower() == 'y':
        main()
    elif reuse.lower() == 'x':
        pass
    else:
        print("I didn't understand that input please type 'Y' or 'X'")
        askRestart()
然后调用函数运行程序:

main()

您还可以简单地将代码包装在
块中,而True:
块中。这将无限重复整个片段

while True:
    choice = int(input("What program would you like to run? \n"))

    #chooses one of the programs
    recarea = 1
    circarea = 2
    fah = 3

    if choice == 1:

        print("This program calculates the area and perimeter of a rectangle:")

        length = float(input("Type in the length: \n"))
        width = float(input("Type in the width: \n"))
        area = length * width
        perimeter = (2*length)+(2*width)

        print("The area of the rectangle is ","{0:.2f}".format(area),"cm and the perimeter is ","{0:.2f}".format(perimeter),"cm",'\n\n')

        reuse = str(input("Would you like to restart the script ?"))

        if reuse == "Yes" or reuse == "yes" or reuse == "y" or reuse == "Y":
            print("Restarting Script...\n")

        else:
            reuse == "No" or reuse == "no" or reuse == "n" or reuse == "N"
            print("Returning to the menu...\n\n")
另一个可能更聪明的选择是,如果输入了某个值,则终止循环。例如,如果输入
0
,则此版本将退出

choice=None
while choice != 0:
    choice = int(input("What program would you like to run? (enter 0 to exit) \n"))

    #chooses one of the programs
    recarea = 1
    circarea = 2
    fah = 3

    if choice == 1:

        print("This program calculates the area and perimeter of a rectangle:")

        length = float(input("Type in the length: \n"))
        width = float(input("Type in the width: \n"))
        area = length * width
        perimeter = (2*length)+(2*width)

        print("The area of the rectangle is ","{0:.2f}".format(area),"cm and the perimeter is ","{0:.2f}".format(perimeter),"cm",'\n\n')

        reuse = str(input("Would you like to restart the script ?"))

        if reuse == "Yes" or reuse == "yes" or reuse == "y" or reuse == "Y":
            print("Restarting Script...\n")

        else:
            reuse == "No" or reuse == "no" or reuse == "n" or reuse == "N"
            print("Returning to the menu...\n\n")

您还可以简单地将代码包装在
块中,而True:
块中。这将无限重复整个片段

while True:
    choice = int(input("What program would you like to run? \n"))

    #chooses one of the programs
    recarea = 1
    circarea = 2
    fah = 3

    if choice == 1:

        print("This program calculates the area and perimeter of a rectangle:")

        length = float(input("Type in the length: \n"))
        width = float(input("Type in the width: \n"))
        area = length * width
        perimeter = (2*length)+(2*width)

        print("The area of the rectangle is ","{0:.2f}".format(area),"cm and the perimeter is ","{0:.2f}".format(perimeter),"cm",'\n\n')

        reuse = str(input("Would you like to restart the script ?"))

        if reuse == "Yes" or reuse == "yes" or reuse == "y" or reuse == "Y":
            print("Restarting Script...\n")

        else:
            reuse == "No" or reuse == "no" or reuse == "n" or reuse == "N"
            print("Returning to the menu...\n\n")
另一个可能更聪明的选择是,如果输入了某个值,则终止循环。例如,如果输入
0
,则此版本将退出

choice=None
while choice != 0:
    choice = int(input("What program would you like to run? (enter 0 to exit) \n"))

    #chooses one of the programs
    recarea = 1
    circarea = 2
    fah = 3

    if choice == 1:

        print("This program calculates the area and perimeter of a rectangle:")

        length = float(input("Type in the length: \n"))
        width = float(input("Type in the width: \n"))
        area = length * width
        perimeter = (2*length)+(2*width)

        print("The area of the rectangle is ","{0:.2f}".format(area),"cm and the perimeter is ","{0:.2f}".format(perimeter),"cm",'\n\n')

        reuse = str(input("Would you like to restart the script ?"))

        if reuse == "Yes" or reuse == "yes" or reuse == "y" or reuse == "Y":
            print("Restarting Script...\n")

        else:
            reuse == "No" or reuse == "no" or reuse == "n" or reuse == "N"
            print("Returning to the menu...\n\n")

欢迎来到StackOverflow!我建议你仔细阅读。如果您的功能在一个函数中,您可以调用该函数重新启动;也许一段时间的循环是最好的。尽管如此,这是一个过于宽泛的问题。查找循环和如上所述的函数。欢迎使用StackOverflow!我建议你仔细阅读。如果您的功能在一个函数中,您可以调用该函数重新启动;也许一段时间的循环是最好的。尽管如此,这是一个过于宽泛的问题。查找循环,如上所述,查找函数。