+ Reply to Thread
Results 1 to 9 of 9
  1. #1
    Sea Torques
    Join Date
    Sep 2006
    Posts
    625
    BG Level
    5

    Intro to programming help with python

    I'm trying to write a simple program using "while" statements. I've done most of the work, I just can't get it to run properly. Here is the assignment:
    Write a program that will calculate the cost of purchasing a meal. This program will include decisions and loops. Details of the program are as follows:
    • 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.
    And this is what I have:
    Spoiler: show
    def main():
    endProgram = "No"
    while endProgram == "No":
    endOrder = "No"
    amountBurger = 0
    amountFry = 0
    amountSoda = 0
    tax = 0
    total = 0
    while endOrder == "No":
    totalBurger = getBurger()
    totalFry = getFry()
    totalSoda = getSoda()
    endOrder = raw_input("End the order? (Yes or No) ")
    priceTotal = calcTotal(totalBurger, totalFry, totalSoda)
    printTotal(priceTotal)
    endProgram = raw_input("Closing time? (End the program? Yes or No)")

    def getBurger():
    amountBurger = input("How many burgers? (0 if none) ")
    priceBurger = amountBurger * .99
    taxBurger = priceBurger * .06
    totalBurger = priceBurger + taxBurger
    return getBurger
    def getFry():
    amountFry = input("How many fries? (0 if none) ")
    priceFry = amountFry * .79
    taxFry = priceFry * .06
    totalFry = priceFry + taxFry
    return totalFry
    def getSoda():
    amountSoda = input("How many Sodas? (0 if none) ")
    priceSoda = amountSoda * 1.09
    taxSoda = priceSoda * .06
    totalSoda = priceSoda + taxSoda
    return totalSoda
    def calcTotal(totalBurger, totalFry, totalSoda):
    priceTotal = totalBurger + totalFry + totalSoda
    return priceTotal
    def printTotal(priceTotal):
    print "Total: $",priceTotal
    return printTotal
    main()


    And this is my error:
    Traceback (most recent call last):
    File "K:\Prog\Lab 5-5.py", line 43, in <module>
    main()
    File "K:\Prog\Lab 5-5.py", line 15, in main
    priceTotal = calcTotal(totalBurger, totalFry, totalSoda)
    File "K:\Prog\Lab 5-5.py", line 38, in calcTotal
    priceTotal = totalBurger + totalFry + totalSoda
    TypeError: unsupported operand type(s) for +: 'function' and 'float'
    Any suggestions how to solve this? I don't even know what this type of coding is called so Google isn't too helpful, and the book includes nothing on using python.

  2. #2
    Relic Shield
    Join Date
    Oct 2006
    Posts
    1,600
    BG Level
    6
    FFXI Server
    Odin

    are you calling main at the bottom to start it? i think this needs to be at the top, or else it is just part of the last function you declared (calctotal). anyway, it looks to me like you are trying to do math on strings. so try this...

    Code:
    main()
    
    def main():
    endProgram = "No"
    while endProgram == "No":
    endOrder = "No"
    amountBurger = 0
    amountFry = 0
    amountSoda = 0
    tax = 0
    total = 0
    while endOrder == "No":
    totalBurger = getBurger()
    totalFry = getFry()
    totalSoda = getSoda()
    endOrder = raw_input("End the order? (Yes or No) ")
    priceTotal = calcTotal(totalBurger, totalFry, totalSoda)
    printTotal(priceTotal)
    endProgram = raw_input("Closing time? (End the program? Yes or No)")
    
    def getBurger():
    amountBurger = input("How many burgers? (0 if none) ")
    priceBurger = int(amountBurger) * .99
    taxBurger = int(priceBurger) * .06
    totalBurger = int(priceBurger) + int(taxBurger)
    return getBurger
    
    def getFry():
    amountFry = input("How many fries? (0 if none) ")
    priceFry = int(amountFry) * .79
    taxFry = int(priceFry) * .06
    totalFry = int(priceFry) + int(taxFry)
    return totalFry
    
    def getSoda():
    amountSoda = input("How many Sodas? (0 if none) ")
    priceSoda = int(amountSoda) * 1.09
    taxSoda = int(priceSoda) * .06
    totalSoda = int(priceSoda) + int(taxSoda)
    return totalSoda
    
    def calcTotal(totalBurger, totalFry, totalSoda):
    priceTotal = int(totalBurger) + int(totalFry) + int(totalSoda)
    return priceTotal
    def printTotal(priceTotal):
    print "Total: $",priceTotal
    return printTotal

  3. #3
    Relic Shield
    Join Date
    Oct 2006
    Posts
    1,600
    BG Level
    6
    FFXI Server
    Odin

    i just noticed python automatically executes main on run, so i believe u can remove that top main()

    ill check back after lunch.

  4. #4
    Sea Torques
    Join Date
    Sep 2006
    Posts
    625
    BG Level
    5

    Getting a different error now.
    File "K:\Prog\Lab 5-5.py", line 15, in main
    priceTotal = int(totalBurger) + int(totalFry) + int(totalSoda)
    TypeError: int() argument must be a string or a number, not 'function'

  5. #5
    Relic Shield
    Join Date
    Oct 2006
    Posts
    1,600
    BG Level
    6
    FFXI Server
    Odin

    try this, your already setting pricetotal in the calctotal function, no need to do pricetotal = calctotal...

    Code:
    def main():
    endProgram = "No"
    while endProgram == "No":
    endOrder = "No"
    amountBurger = 0
    amountFry = 0
    amountSoda = 0
    tax = 0
    total = 0
    while endOrder == "No":
    totalBurger = getBurger()
    totalFry = getFry()
    totalSoda = getSoda()
    endOrder = raw_input("End the order? (Yes or No) ")
    calcTotal(totalBurger, totalFry, totalSoda)
    printTotal(priceTotal)
    endProgram = raw_input("Closing time? (End the program? Yes or No)")
    
    def getBurger():
    amountBurger = input("How many burgers? (0 if none) ")
    priceBurger = int(amountBurger) * .99
    taxBurger = int(priceBurger) * .06
    totalBurger = int(priceBurger) + int(taxBurger)
    return getBurger
    
    def getFry():
    amountFry = input("How many fries? (0 if none) ")
    priceFry = int(amountFry) * .79
    taxFry = int(priceFry) * .06
    totalFry = int(priceFry) + int(taxFry)
    return totalFry
    
    def getSoda():
    amountSoda = input("How many Sodas? (0 if none) ")
    priceSoda = int(amountSoda) * 1.09
    taxSoda = int(priceSoda) * .06
    totalSoda = int(priceSoda) + int(taxSoda)
    return totalSoda
    
    def calcTotal(totalBurger, totalFry, totalSoda):
    priceTotal = int(totalBurger) + int(totalFry) + int(totalSoda)
    return priceTotal
    def printTotal(priceTotal):
    print "Total: $",priceTotal
    return printTotal

  6. #6
    Sea Torques
    Join Date
    Sep 2006
    Posts
    625
    BG Level
    5

    It's working now, I appreciate the help.

  7. #7
    Relic Shield
    Join Date
    Oct 2006
    Posts
    1,600
    BG Level
    6
    FFXI Server
    Odin

    was just looking thru it again, you can just call all the functions without assigning them to a variable, since the functions itself is doing it. also your getburger is returning it to the wrong variable...

    Code:
    def main():
    endProgram = "No"
    while endProgram == "No":
    endOrder = "No"
    amountBurger = 0
    amountFry = 0
    amountSoda = 0
    tax = 0
    total = 0
    while endOrder == "No":
    getBurger()
    getFry()
    getSoda()
    endOrder = raw_input("End the order? (Yes or No) ")
    calcTotal(totalBurger, totalFry, totalSoda)
    printTotal(priceTotal)
    endProgram = raw_input("Closing time? (End the program? Yes or No)")
    
    def getBurger():
    amountBurger = input("How many burgers? (0 if none) ")
    priceBurger = int(amountBurger) * .99
    taxBurger = int(priceBurger) * .06
    totalBurger = int(priceBurger) + int(taxBurger)
    return totalBurger
    
    def getFry():
    amountFry = input("How many fries? (0 if none) ")
    priceFry = int(amountFry) * .79
    taxFry = int(priceFry) * .06
    totalFry = int(priceFry) + int(taxFry)
    return totalFry
    
    def getSoda():
    amountSoda = input("How many Sodas? (0 if none) ")
    priceSoda = int(amountSoda) * 1.09
    taxSoda = int(priceSoda) * .06
    totalSoda = int(priceSoda) + int(taxSoda)
    return totalSoda
    
    def calcTotal(totalBurger, totalFry, totalSoda):
    priceTotal = int(totalBurger) + int(totalFry) + int(totalSoda)
    return priceTotal
    def printTotal(priceTotal):
    print "Total: $",priceTotal
    return printTotal

  8. #8
    Sea Torques
    Join Date
    Sep 2006
    Posts
    625
    BG Level
    5

    Ah yes, thank you.

  9. #9
    Smells like Onions
    Join Date
    Feb 2010
    Posts
    1
    BG Level
    0

    What did you guys do to make this source work? I to am taken the same class and this messed up Yum Yum is bs, I never want a burger ever

Similar Threads

  1. Help with python 2.6
    By MiHavens in forum Tech
    Replies: 2
    Last Post: 2009-11-07, 20:17
  2. Help with converting .MTS (AVCHD) to .wmv
    By Rockstaru in forum Tech
    Replies: 2
    Last Post: 2009-02-02, 18:56