Python Forum

Full Version: Final problem
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
My task is to write a code that will add integers until user decides to quit the program with "Q". The program has two modules that user can choose - in the first ("A") prints each number and their total and in the second ("T") it prints only total.

def adding_report(choose):
    total = 0
    print("Enter an integer to add to the total or Quit.")
    while True:
        
        if choose == "A":
            
            numb = input('Enter an integer or "Q": ')
            if numb.isdigit():
                total = total + int(numb)
                
            elif numb.startswith("Q"):
                print("Items")
                print("Total")
                print(total)
                
            else:
                print("Invalid input!")
                
        if choose == "T":
            
            numb = input('Enter an integer or "Q": ')
            if numb.isdigit():
                total = total + int(numb)
                
            elif numb.startswith("Q"):
                print("Total")
                print(total)
                
            else:
                print("Invalid input!")
                
adding_report(input("Choose report type(A or T): "))


I wrote the program, the only problem is when opting for module "A" I don't know how to print each numb in elif statement before print("Items"). For example if I input 4, 5, 10 before "Q" and print(num) above line print(Total) it will only print me 10 and I want 4,5,10.
Ideas are appreciated.
Simplest solution is to have a list and append value to the list in each iteration. List is a basic data type and can be printed simply like all others.
def adding_report(choose):
    total = 0
    lista = []
    print("Enter an integer to add to the total or Quit.")
    while True:
         
        if choose == "A":
             
            numb = input('Enter an integer or "Q": ')
            lista.append(numb)
            if numb.isdigit():
                total = total + int(numb)
                 
            elif numb.startswith("Q"):
            
                print("Items")
                print(lista)
                print("Total")
                print(total)
                 
            else:
                print("Invalid input!")
                 
        if choose == "T":
             
            numb = input('Enter an integer or "Q": ')
            if numb.isdigit():
                total = total + int(numb)
                 
            elif numb.startswith("Q"):
                print("Total")
                print(total)
                 
            else:
                print("Invalid input!")
                 
adding_report(input("Choose report type(A or T): "))
output is now ['2', '3', '4', 'Q']. I still don't understand how to manage printing of every input.
I tried with
lista = ''
.
.
.
lista.append(numb)
...but that gives error, something about string.
lista = '' - this means you declared lista to be a string, and string doesn't have append method, while list does.

If you don't want Q in your list, put lista.append(numb) inside if numb.isdigit(): statement.
def adding_report(choose):
    total = 0
    lista = []
    print("Enter an integer to add to the total or Quit.")
    while True:
          
        if choose == "A":
              
            numb = input('Enter an integer or "Q": ')
            
            if numb.isdigit():
                total = total + int(numb)
                lista.append(numb)  
            elif numb.startswith("Q"):
             
                print("Items")
                print(lista)
                print("Total")
                print(total)
                  
            else:
                print("Invalid input!")
                  
        if choose == "T":
              
            numb = input('Enter an integer or "Q": ')
            if numb.isdigit():
                total = total + int(numb)
                  
            elif numb.startswith("Q"):
                print("Total")
                print(total)
                  
            else:
                print("Invalid input!")
                  
adding_report(input("Choose report type(A or T): "))
edit: Change made, now it works fine!