Python Forum

Full Version: Difficulty when trying to call all the input values
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

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    
How many different numbers can you store simultaneously in one variable ?
If you need more than one, you need ...
Also, it seemed to me there is an adding problem. 1 + 1 did not add up to 2 .
Paul
(Jun-05-2020, 05:50 AM)extricate Wrote: [ -> ]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.
You can write your code in any way you want - it's your choice. However, a function is a block of organized, reusable code that is used to perform a single, related action. Functions provide better modularity for your application and a high degree of code reusing. As you already know, Python gives you many built-in functions like print(), etc. but you can also create your own functions.