Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Final problem
#1
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.
Reply
#2
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.
Reply
#3
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.
Reply
#4
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.
Reply
#5
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!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Smile Final Projet - Credit Risk Rauchvant 3 2,374 Nov-18-2020, 03:21 PM
Last Post: Rauchvant
  Help needed, stuck at the final output extricate 0 1,513 Jun-20-2020, 05:35 AM
Last Post: extricate
  Final Project (Databases, GUI, and Classes) poochenthecreator 1 1,616 Apr-27-2020, 09:58 PM
Last Post: deanhystad
  Final Project Help hyg71886 6 3,884 Feb-07-2019, 01:30 AM
Last Post: micseydel
  Trouble with edX Python Final sarah_mb_sues 11 13,611 Jun-19-2018, 10:36 AM
Last Post: cryomick

Forum Jump:

User Panel Messages

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