Python Forum

Full Version: python program problem
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
please give a runnable sample of your code with the full error text or a clear description of the problem

IM RUN THIS CODE

    total = 0

    def adding_report(report = "T"):
        while True:
            integer = input("Input an integer to add to the total or 'Q' to quit: ")
            total = total + integer
            items = (integer/n)
            if integer.isdigit():
                if report == "A":
                    print("Items"/n/n, Integer)
                    break
                elif report == "T":
                    print(total)
                    break
            elif integer.startswith().lower("Q"):
                break
            else:
                print("Input is invalid")
        return report

    adding_report("A")
AND THEN I GET THIS ERROR

Input an integer to add to the total or 'Q' to quit: 5
---------------------------------------------------------------------------
UnboundLocalError Traceback (most recent call last)
<ipython-input-2-7df26b21c5bc> in <module>()
19 return report
20
---> 21 adding_report("A")
22

<ipython-input-2-7df26b21c5bc> in adding_report(report)
4 while True:
5 integer = input("Input an integer to add to the total or 'Q' to quit: ")
----> 6 total = total + integer
7 items = (integer/n)
8 if integer.isdigit():

UnboundLocalError: local variable 'total' referenced before assignment

WHAT AM I DOING WRONG?
add (right after your def for adding_report)
    total = 0
The one you have now is out of scope once you enter the adding_report function
Ok Thanks! Is that the only thing wrong with it? Im looking for a result like this:

call adding_report() with "A" as argument (print all the integers entered and the total)

Input an integer to add to the total or "Q" to quit
Enter an integer or "Q": 3
Enter an integer or "Q": 6
Enter an integer or "Q": 24
Enter an integer or "Q": 17
Enter an integer or "Q": 61
Enter an integer or "Q": nine
nine is invalid input
Enter an integer or "Q": q

Items
3
6
24
17
61

Total
111
call with "T"(print only the total)

Input an integer to add to the total or "Q" to quit
Enter an integer or "Q": 5
Enter an integer or "Q": 7
Enter an integer or "Q": Quit

Total
12
No, you have lots of other errors. You haven't defined n. The '/n/n' on line 10 makes no sense, I'm not sure what you are trying to do there. Printing the total should be done after loop, not each time through the loop. You've got startswith and lower backwards, and what you are looking for should go with startswith (and since you are using lower, it should be lower case).
the n\ is newline. So that it prints off the returns in a list after all the input statements. Im not sure how to do that.
For a new line, use \n within the quotes.