Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.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.x 在多个子例程中使用参数_Python 3.x - Fatal编程技术网

Python 3.x 在多个子例程中使用参数

Python 3.x 在多个子例程中使用参数,python-3.x,Python 3.x,我目前正在尝试编写一个程序,其中用户输入他们想要的地毯颜色,然后根据他们输入的地毯颜色和区域,它将为他们提供不同的价格,但是我当前的问题是正确使用参数,因为我对使用python和编程都非常陌生。当前的程序规范要求使用子程序。例如,我的最后一行main(exit1)中有一个问题,它说exit1没有定义,如果我试图编辑main()的代码,它说exit1是必需的。任何帮助都将不胜感激 def prices(): PriceA = 40 PriceB = 50 PriceC =

我目前正在尝试编写一个程序,其中用户输入他们想要的地毯颜色,然后根据他们输入的地毯颜色和区域,它将为他们提供不同的价格,但是我当前的问题是正确使用参数,因为我对使用python和编程都非常陌生。当前的程序规范要求使用子程序。例如,我的最后一行main(exit1)中有一个问题,它说exit1没有定义,如果我试图编辑main()的代码,它说exit1是必需的。任何帮助都将不胜感激

def prices():
    PriceA = 40
    PriceB = 50
    PriceC = 60
def main(exit1):
    exit1 = False
    while exit1 == False:
        carpet = str(input("What carpet would you like blue, red or yellow "))
        carpet = carpet.upper()
        if carpet == "X":
            exit1 = True
        else:
            width = int(input("What is the width of the room in M^2 "))
            height = int(input("What is the height of the room in M^2 "))
            area = width * height   


            if carpet == "BLUE":          
                a(area)

            elif carpet == "RED":
                b(area)

            elif carpet == "YELLOW":
                c(area)


            else:
                print("Invalid carpet")
                cost = 0
                output(cost)





def output(cost, exit1):
print ("the price is £", cost)


def a(area, PriceA):
    cost = PriceA * area
    output(cost)

def b(area, PriceB):
    cost = PriceB * area
    output(cost)

def c(area, PriceC):
    cost = PriceC * area
    output(cost)

main(exit1)

您试图将变量作为参数传入程序最后一行的
main
函数,但在执行该行之前尚未定义
exit1
变量(您在
main
函数的范围内定义它)。要实现您想要做的事情,您不需要为
main
提供
exit1
参数,因此您可以将其从函数定义和函数调用中删除

def main():
    ...

...

main()

您试图将变量作为参数传入程序最后一行的
main
函数,但在执行该行之前尚未定义
exit1
变量(您在
main
函数的范围内定义它)。要实现您想要做的事情,您不需要为
main
提供
exit1
参数,因此您可以将其从函数定义和函数调用中删除

def main():
    ...

...

main()

您遇到的问题是,在将变量输入函数之前,需要先定义变量。我修复了代码并做了一些更正。另外,似乎您在调用函数时出现了缩进错误,并且没有向函数中输入足够的参数。我写的代码更容易理解。如果我做了任何你不理解的事情,请给我发一条消息,否则我推荐CodeAcadamy和pythonprogramming.net等网站。 def main():


您遇到的问题是,在将变量输入函数之前,需要先定义变量。我修复了代码并做了一些更正。另外,似乎您在调用函数时出现了缩进错误,并且没有向函数中输入足够的参数。我写的代码更容易理解。如果我做了任何你不理解的事情,请给我发一条消息,否则我推荐CodeAcadamy和pythonprogramming.net等网站。 def main():