Python Forum
Difficulty when trying to call all the input values
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Difficulty when trying to call all the input values
#1
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    
Reply
#2
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
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Reply
#3
(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.
pyzyx3qwerty
"The greatest glory in living lies not in never falling, but in rising every time we fall." - Nelson Mandela
Need help on the forum? Visit help @ python forum
For learning more and more about python, visit Python docs
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Input values and write csv eltoro 1 2,045 Mar-08-2021, 12:56 AM
Last Post: jefsummers
  4 tries choice question, one difficulty extricate 4 2,385 Jun-04-2020, 05:07 AM
Last Post: extricate
  Writing python function difficulty kirito85 5 3,294 Oct-28-2018, 07:34 AM
Last Post: buran
  Increasing difficulty of a maths game Maxxy_Gray 1 3,189 Apr-04-2018, 03:00 PM
Last Post: sparkz_alot

Forum Jump:

User Panel Messages

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