I am trying to write a program that collects gas receipt information. I only want it to ask one time for how many receipts there are and then I want it go ask the loop of questions.

How do I do this? I'm guessing I can't declare multiple variables in the beginning? We did not learn much in my class, so I'm trying to go above and beyond and do a little more for our project to get the program where/how I want it.

Here is my starting off code:

#the main function
def main():
    milesObtained = getMiles() #call to get the miles obtained
    totalPrice, totalGallons = getInput() #calls to collect total prices paid
    printReport(milesObtained, totalPrice, totalGallons) #call to print the full report out
    
#collects miles obtained during the month
def getMiles():
    milesStarted = input('Enter the starting miles: ')
    milesEnded = input('Enter the ending miles: ')
    milesObtained = milesEnded - milesStarted
    return milesObtained

#collects receipt data
def getInput():
    totalPrice = 0
    totalGallons = 0
    number = input('How many receipts do you have to enter? ')
    for counter in range (0, number):
        price = input('Enter the price: ')
        gallons = input('Enter the gallon amount: ')
        totalGallons = totalGallons + gallons
        totalPrice = totalPrice + price
    return totalPrice
    return totalGallons
        
#prints out the report
def printReport(milesObtained, totalPrice, totalGallons):
    print 
    print '----------------------------'
    print 'RESULTS OF GAS THIS MONTH'
    print '----------------------------'
    print 'The miles obtained were: ', milesObtained
    print 'The total price paid this month is: ', totalPrice
    print 'The total amount of gallons this month is: ', totalGallons

#calls main
main()

When I run the code, it does this:

IDLE 1.2.2      ==== No Subprocess ====
>>> 
Enter the starting miles: 105000
Enter the ending miles: 107000
How many receipts do you have to enter? 2
Enter the price: 10.00
Enter the gallon amount: 2
Enter the price: 10.00
Enter the gallon amount: 2
Traceback (most recent call last):
  File "C:\Users\fmcgovern\Documents\School Work\Programming\ProjectTest.py", line 38, in <module>
    main()
  File "C:\Users\fmcgovern\Documents\School Work\Programming\ProjectTest.py", line 4, in main
    totalPrice, totalGallons = getInput() #calls to collect total prices paid
TypeError: 'float' object is not iterable
>>>

Dani AI

Generated

Quick diagnosis and why the error happened: the script calls for two values to be unpacked (the main call expects both a price total and a gallons total) but the receipt-collection function ended up returning a single float. The traceback "'float' object is not iterable" means Python tried to unpack or iterate a non-iterable value. was on the right track — the caller expects a grouped return but the function only gave back one value (the second return in that function was never reached). (stackoverflow.com)

Concrete fixes and checks to make the behavior predictable: return the two totals as a single compound value (for example a tuple, a dict, or a namedtuple) so the caller can unpack or index the results; make sure that the function has one return that contains both results rather than two isolated returns (a return leaves the function immediately). Convert user input to the right numeric types before summing (use explicit casts such as int() / float() or, in Python 2, call raw_input() then cast) and ensure the receipt count is an integer before using it with range(). These patterns (single return value carrying multiple items and sequence unpacking) are standard Python behavior. (docs.python.org)

Extra tips and modern best practice: add simple validation (wrap conversions in try/except to handle bad input), print the type() of the value returned from the function while debugging, or return a small mapping / namedtuple for clarity instead of positional tuple values. Consider running the script under Python 3 (input semantics are clearer there: raw_input() was renamed to input() in Py3) to avoid surprises with evaluated input in older interpreters. (docs.python.org)

Recommended Answers

All 3 Replies

Just a a quick look. Read the comments ...

#the main function
def main():
    milesObtained = getMiles() #call to get the miles obtained
    # expect the return of one totalPrice, totalGallons tuple
    totalPrice, totalGallons = getInput() #calls to collect total prices paid
    printReport(milesObtained, totalPrice, totalGallons) #call to print the full report out
    
#collects miles obtained during the month
def getMiles():
    milesStarted = input('Enter the starting miles: ')
    milesEnded = input('Enter the ending miles: ')
    milesObtained = milesEnded - milesStarted
    return milesObtained

#collects receipt data
def getInput():
    totalPrice = 0
    totalGallons = 0
    number = input('How many receipts do you have to enter? ')
    for counter in range (0, number):
        price = input('Enter the price: ')
        gallons = input('Enter the gallon amount: ')
        totalGallons = totalGallons + gallons
        totalPrice = totalPrice + price
    # return one totalPrice, totalGallons tuple
    return totalPrice, totalGallons
        
#prints out the report
def printReport(milesObtained, totalPrice, totalGallons):
    print 
    print '----------------------------'
    print 'RESULTS OF GAS THIS MONTH'
    print '----------------------------'
    print 'The miles obtained were: ', milesObtained
    print 'The total price paid this month is: ', totalPrice
    print 'The total amount of gallons this month is: ', totalGallons

#calls main
main()

I'm not sure I get what you're stating in the comments? If I put what you added and remove the comments, it gives me a syntax error. If you show me what the code should be, I will be able to understand. I just don't think we've learned this much and are not going to, so my knowledge is short on coming up with the answer.

Can you give us the content of your error message?

totalPrice, totalGallons or (totalPrice, totalGallons) is a tuple
your function has to return that to match the function call result

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.