ich bringe mir gerade Python beide und löse verschiedene Aufgaben. Ich habe meine eine Aufgabe fast zufriedenstellend gelöst, bis ich bemerkt habe das ich das alles in eine Funktion packen sollte.
Code: Alles auswählen
total = 0
item_add = ""
items = "Items\n "
report = ""
def adding_report(report):
report = input("Chose Report Type (\"A\" or \"T\"):")
while True:
item_add = input("Enter an integer or \"Q\"")
if item_add.isdigit():
total = total + int(item_add)
items = items + item_add + "\n "
elif item_add == "Q" :
if report == "A":
print(items)
print("Total\n" + str(total))
break
elif report == "T":
print("Total\n" + str(total))
break
else:
print(item_add + " " + "is invalid input")
print("Report Types include All Items (\"A\" or Total Only \"T\")")
adding_report(report)
Traceback (most recent call last):
File "python", line 42, in <module> #???
File "python", line 15, in adding_report #???
UnboundLocalError: local variable 'total' referenced before assignment #ich hab doch die variable total gleich am Anfang platziert?
Für etwas Erleuchtung wäre ich sehr dankbar!
Gruß Odox