Jun-05-2020, 05:50 AM
Hi,
Below is my code:
"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"
But my code can't seem to call all the numbers under the header "Items", it only calls the last number that I've inputed. Any idea what's wrong?
Thank you
I've managed to solve it
I'd like to ask a question
I've also managed to create a code without def function, what's the purpose of creating a function then? I don't seem to really understand in depth even having attended the microsoft course.
Below is my code:
def add_report(report): total = 0 item = "" user = input("Enter an integer to add to toal or Q") while True: if report.upper() == "T": user_1 = input("Enter integer or Q to quit") if user_1.isdigit(): total = int(user_1) + total elif user_1.upper() == "Q": print("\nTotal:\n",total) break elif report.upper() == "A": user_1 = input("Enter integer or Q to quit") if user_1.isdigit(): total = int(user_1) + total item = user_1 elif user_1.upper() == "Q": print("\nItems\n",item,"\n","Total: ","\n",total) break else: print("Invalid input") add_report("A")The code is supposed to call all the values that I've input, for eg:
"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"
But my code can't seem to call all the numbers under the header "Items", it only calls the last number that I've inputed. Any idea what's wrong?
Thank you
I've managed to solve it
I'd like to ask a question
I've also managed to create a code without def function, what's the purpose of creating a function then? I don't seem to really understand in depth even having attended the microsoft course.
total = 0 items = "" choice = input("Enter T or A") while True: if choice == "T": integer = input("Enter integer or Q to quit") if integer.isdigit() == True: total = int(integer) + total elif integer.upper() == "Q": print("\nTotal:\n",total) break elif choice == "A": integer = input("Enter integer or Q to quit") if integer.isdigit() == True: total = int(integer) + total items = items + "\n" + integer elif integer.upper() == "Q": print("Items: \n",items,"\n","Total: ","\n",total) break