Python 2.7多重错误的伪代码



我目前正在做家庭作业,不能理解我在代码转换中做错了什么。我有多个问题,这是在我的代码下描述的。分配的问题和伪代码如下:

>Write python code for the following Burger Joint:
>•  Your menu items only include the following food with accompanied price: 
>o  Yum Yum Burger = .99
>o  Grease Yum Fries = .79
>o  Soda Yum = 1.09
>•  Allow the user of the program to purchase any quantity of these items on one order. 
>•  Allow the user of the program to purchase one or more types of these items on one >order.
>•  After the order is placed, calculate total and add a 6% sales tax. 
>•  Print to the screen a receipt showing the total purchase price.
下面是伪代码:
Module main()
Call declareVariables(endProgram, endOrder, totalBurger, totalFry, totalSoda, total, tax, subtotal, option, burgerCount, fryCount, sodaCount)
//Loop to run program again
While endProgram == “no”
    Call resetVariables(totalBurger, totalFry, totalSoda, total, tax, subtotal)     
    //Loop to take in order
    While endOrder == “no”
        Display “Enter 1 for Yum Yum Burger”
        Display “Enter 2 for Grease Yum Fries”
        Display “Enter 3 for Soda Yum”
        Input option
        If option == 1 Then
            Call getBurger(totalBurger, burgerCount)
        Else If option == 2 Then
            Call getFry(totalFry, fryCount)
        Else If option == 3 Then
            Call getSoda(totalSoda, sodaCount)
        End If
Display “Do you want to end your order? (Enter no to add more items: )”
Input endOrder
    End While
Call calcTotal(burgerTotal, fryTotal, sodaTotal, total, subtotal, tax)
Call printReceipt(total)
Display “Do you want to end the program? (Enter no to process a new order)”
Input endProgram
End While
End Module
Module declareVariables(String Ref endProgram, String Ref endOrder, Real Ref totalBurger, Real Ref totalFry, Real Ref totalSoda, Real Ref  total, Real Ref tax, Real Ref subtotal, Real Ref option, Real Ref burgerCount, Real Ref fryCount, Real Ref sodaCount)
Declare String endProgram = “no”
Declare String endOrder = “no”
Declare Real totalBurger = 0
Declare Real totalFry = 0
Declare Real totalSoda = 0
Declare Real total = 0
Declare Real tax = 0
Declare Real subtotal = 0
Declare Integer option = 0
Declare Integer burgerCount = 0
Declare Integer fryCount = 0
Declare Integer sodaCount = 0
End Module
Module resetVariables (Real Ref totalBurger, Real Ref totalFry, Real Ref totalSoda, Real Ref total, Real Ref tax, Real Ref subtotal)
//reset variables
totalBurger = 0
totalFry = 0
totalSoda = 0
total = 0
tax = 0
subtotal = 0
End Module
Module getBurger(Real Ref totalBurger, Integer burgerCount)
Display “Enter the number of burgers you want”
Input burgerCount   
Set totalBurger = totalBurger + burgerCount * .99
End Module
Module getFry(Real Ref totalFry, Integer fryCount)
Display “Enter the number of fries you want”
Input fryCount  
Set totalFry = totalFry + fryCount * .79
End Module
Module getSoda(Real Ref totalSoda, Integer sodaCount)
Display “Enter the number of sodas you want”
Input sodaCount 
Set totalSoda = totalSoda + sodaCount * 1.09
End Module
Module calcTotal(Real totalBurger, Real totalFry, Real totalSoda, Real Ref total, Real subtotal, Real tax)
Set subtotal = totalBurger + totalFry + totalSoda
Set tax = subtotal * .06
Set total = subtotal + tax
End Module
Module printReceipt(Real total)
Display “Your total is $”, total
End Module

这是我目前为止的python代码(python 2.7):

def main():
    declareVariables(endProgram, endOrder, totalBurger, totalFry, totalSoda, total, tax, subtotal, option, burgerCount, fryCount, sodaCount)
    ##Loop to run program again
    while endProgram == "no":
        resetVariables(totalBurger, totalFry, totalSoda, total, tax, subtotal)

        ##Loop to take in order
        while endOrder == "no":
            print "Enter 1 for Yum Yum Burger"
            print "Enter 2 for Grease Yum Fries"
            print "Enter 3 for Soda Yum"
            option = input("Enter Now: ")
            if option == 1:
                getBurger(totalBurger, burgerCount)
            elif option == 2:
                getFry(totalFry, fryCount)
            elif option == 3:
                getSoda(totalSoda, sodaCount)
            endOrder = raw_input ("Do you want to end your order? (Enter no to add more items): ")
        calcTotal(totalBurger, totalFry, totalSoda, total, subtotal, tax)
        printReceipt(total)
        endProgram = raw_input("Do you want to end the program? (Enter no to process a new order)")
def declareVariables(endProgram, endOrder, totalBurger, totalFry, totalSoda, total, tax, subtotal, option, burgerCount, fryCount, sodaCount):
    endProgram = "no"
    endOrder = "no"
    totalBurger = 0
    totalFry = 0
    totalSoda = 0
    total = 0
    tax = 0
    subtotal = 0
    option = 0
    burgerCount = 0
    fryCount = 0
    sodaCount = 0
def resetVariables(totalBurger, totalFry, totalSoda, total, tax, subtotal):
    totalBurger = 0
    totalFry = 0
    totalSoda = 0
    total = 0
    tax = 0
    subtotal = 0

def getBurger(totalBurger, burgerCount):
    burgerCount = input ("Enter the number of burgers you want: ")
    totalBurger = totalBurger + burgerCount *.99
    return totalBurger

def getFry(totalFry, fryCount):
    fryCount = input ("Enter the number of fries you want: ")
    totalFry = totalFry + fryCount * .79
    return totalFry

def getSoda(totalSoda, sodaCount):
    sodaCount = input ("Enter the number of sodas you want: ")
    totalSoda = totalSoda + sodaCount * 1.09
    return totalSoda

def calcTotal(totalBurger, totalFry, totalSoda, total, subtotal, tax):
    subtotal = totalBurger + totalFry + totalSoda
    tax = subtotal * .06
    total = subtotal + tax
    return total

def printReceipt(total):
    print "Your total is $", total
main()

这些是我不知道如何工作过去的问题:

  1. 赋值前引用的局部变量endProgram。这是我第一次看到从模块调用变量初始化。该模块在开始时被调用,因此看起来应该将它们全部初始化。我错过了什么?
  2. 如果我注释掉declarevariable模块,只是用手动赋值创建全局变量,代码将运行,但它总是返回收据总额$0,就好像参数不是通过引用传递的一样。不确定如何使其接受模块中的引用。
谁能帮我指一下正确的方向吗?我觉得我离得很近,却忽略了一些很简单的东西。

注意:我在谷歌和论坛上搜索了一下,实际上在其他地方发现了这个确切的问题。然而,在本例中,教授构造了伪代码,而不是允许我们这样做。我想我会用不同的方式解决这个问题,但是python代码必须与教授的pseudo相匹配才能获得学分。

谢谢你提供的任何帮助。

编辑:谢谢你的指点。我将代码更改为以下代码,但当我运行编辑后的代码时,收据总额仍然为0美元。这部分有什么方向吗?

def main():
    endProgram = "no"
    endOrder = "no"
    totalBurger = 0
    totalFry = 0
    totalSoda = 0
    total = 0
    tax = 0
    subtotal = 0
    option = 0
    burgerCount = 0
    fryCount = 0
    sodaCount = 0
    ##Loop to run program again
    while endProgram == "no":

        ##Loop to take in order
        while endOrder == "no":
            print "Enter 1 for Yum Yum Burger"
            print "Enter 2 for Grease Yum Fries"
            print "Enter 3 for Soda Yum"
            option = input("Enter Now: ")
            if option == 1:
                getBurger(totalBurger, burgerCount)
            elif option == 2:
                getFry(totalFry, fryCount)
            elif option == 3:
                getSoda(totalSoda, sodaCount)
            endOrder = raw_input ("Do you want to end your order? (Enter no to add more items): ")
        calcTotal(totalBurger, totalFry, totalSoda, total, subtotal, tax)
        printReceipt(total)
        endProgram = raw_input("Do you want to end the program? (Enter no to process a new order)")

def getBurger(totalBurger, burgerCount):
    burgerCount = input ("Enter the number of burgers you want: ")
    totalBurger = totalBurger + burgerCount *.99
    return totalBurger

def getFry(totalFry, fryCount):
    fryCount = input ("Enter the number of fries you want: ")
    totalFry = totalFry + fryCount * .79
    return totalFry

def getSoda(totalSoda, sodaCount):
    sodaCount = input ("Enter the number of sodas you want: ")
    totalSoda = totalSoda + sodaCount * 1.09
    return totalSoda

def calcTotal(totalBurger, totalFry, totalSoda, total, subtotal, tax):
    subtotal = totalBurger + totalFry + totalSoda
    tax = subtotal * .06
    total = subtotal + tax
    return total

def printReceipt(total):
    print "Your total is $", total
main()

最后编辑:

如果有人感兴趣的话,这是我被指示按照教授完成作业的方式。谢谢大家的帮助。

def main():
    ## Initialize Variables
    endProgram, endOrder, totalBurger, totalFry, totalSoda, total, tax, subtotal, option, burgerCount, fryCount, sodaCount = declareVariables()

    ##Loop to run program again
    while endProgram == "no":
        #Reset variables
        endOrder, totalBurger, totalFry, totalSoda, total, tax, subtotal = resetVariables()
        ##Loop to take in order
        while endOrder == "no":
            print "Enter 1 for Yum Yum Burger"
            print "Enter 2 for Grease Yum Fries"
            print "Enter 3 for Soda Yum"
            option = input("Enter Now: ")
            if option == 1:
                totalBurger = getBurger(totalBurger, burgerCount)
            elif option == 2:
                totalFry = getFry(totalFry, fryCount)
            elif option == 3:
                totalSoda = getSoda(totalSoda, sodaCount)
            endOrder = raw_input ("Do you want to end your order? (Enter no to add more items): ")
        total = calcTotal(totalBurger, totalFry, totalSoda, total, subtotal, tax)
        printReceipt(total)
        endProgram = raw_input("Do you want to end the program? (Enter no to process a new order)")

## Calculate the total cost of burgers and return totalBurger
def getBurger(totalBurger, burgerCount):
    burgerCount = input ("Enter the number of burgers you want: ")
    totalBurger = totalBurger + ( burgerCount *.99 )
    return totalBurger
## Calculate the total cost of fries and return totalFry
def getFry(totalFry, fryCount):
    fryCount = input ("Enter the number of fries you want: ")
    totalFry = totalFry + ( fryCount * .79 )
    return totalFry
## Calculate the total cost of soda and return totalSoda
def getSoda(totalSoda, sodaCount):
    sodaCount = input ("Enter the number of sodas you want: ")
    totalSoda = totalSoda + ( sodaCount * 1.09 )
    return totalSoda
## Calculate the total cost of burgers, fries, & soda. Add tax and returns the total. 
def calcTotal(totalBurger, totalFry, totalSoda, total, subtotal, tax):
    subtotal = totalBurger + totalFry + totalSoda
    tax = subtotal * .06
    total = subtotal + tax
    return total
## Print the receipt for the total cost.
def printReceipt(total):
    print "Your total is $", total
## Initialize all of the variables and return all of them
def declareVariables():
     endProgram = "no"
     endOrder = "no"
     totalBurger = 0
     totalFry = 0
     totalSoda = 0
     total = 0
     tax = 0
     subtotal = 0
     option = 0
     burgerCount = 0
     fryCount = 0
     sodaCount = 0
     return endProgram, endOrder, totalBurger, totalFry, totalSoda, total, tax, subtotal, option, burgerCount, fryCount, sodaCount
## Reset the variables for when the order ends but program continues to start a new order.
## Return all of the reset variables
def resetVariables():
    endOrder = "no"
    totalBurger = 0
    totalFry = 0
    totalSoda = 0
    total = 0
    tax = 0
    subtotal = 0
    return endOrder, totalBurger, totalFry, totalSoda, total, tax, subtotal

main()

1。

在使用你打算使用的函数/变量之前声明它们。

将所有方法的定义放在调用它们的 main之前。例如,您的main在执行时不知道declareVariables函数是什么,因此出现异常。

2。全局变量或返回变量

变量的引用不像C/c++那样工作。你可能想看看这个。您可以将已更改的变量声明为return或将它们声明为global(这可能是最简单的)。你可以在这里找到更多信息

旁注,变量实际上并不保存数据,而更像是标签。所以a = b是复制标签到同一对象。你可以自己试试:

>>a = [1,2,3]
>>b = a  # This isn't copying values, but labels
>>b.append(100)  # Changing value of 'b' would change the underlying object, thereby changing all referenced objects
>>print a
[1, 2, 3, 100]

最新更新