Python Forum
python program problem
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
python program problem
#1
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?
Reply
#2
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
Reply
#3
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
Reply
#4
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).
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
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.
Reply
#6
For a new line, use \n within the quotes.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python program problem beemanjo 4 3,511 Sep-27-2017, 01:40 AM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020