|
| 1 | +#This program calls the adding_report() function which repeatedly takes positive integer input until the user quits and then sums the integers and prints a "report". |
| 2 | + |
| 3 | +#"A" used as the argument to adding_report() results in printing of all of the input integers and the total |
| 4 | +#"T" used as the argument results in printing only the total |
| 5 | + |
| 6 | +print("Add an 'A' to see the input list and total or a 'T' to see just the total.") |
| 7 | + #checks for A or T |
| 8 | +while True: |
| 9 | + report_type = input("A/T: ").upper() |
| 10 | + if report_type not in ("A,T"): |
| 11 | + print("Invalid Entry") |
| 12 | + else: |
| 13 | + break |
| 14 | + |
| 15 | + #The adding_report() function has 1 string parameter which indicates the type of report: |
| 16 | +def adding_report(report): |
| 17 | + #initialize total variable which will sum integer values entered |
| 18 | + #initialize items variable which will build a string of the integer inputs separated with a new line character |
| 19 | + items = "" |
| 20 | + total=0 |
| 21 | + add_integer = "" |
| 22 | + #inside the function build a forever loop (infinite while loop) and inside the loop complete the following |
| 23 | + while True: |
| 24 | + #use a variable to gather input (integer or "Q") |
| 25 | + add_integer = input("Enter an integer of 'Q' to quit: ").upper() |
| 26 | + if add_integer.isdigit(): |
| 27 | + #check if the input string is a digit (integer) and if it is... |
| 28 | + items += '\n' + add_integer #if report type is "A" add the numeric character(s) to the item string seperated by a new lin |
| 29 | + #add input iteger to total |
| 30 | + total += int(add_integer) |
| 31 | + elif add_integer.upper() == "Q":#if not a digit, check if the input string is "Q" |
| 32 | + #if the report type is "A" print out all the integer items entered and the sum total |
| 33 | + if report_type.upper() == "A": |
| 34 | + print() |
| 35 | + print("items:",items) |
| 36 | + break |
| 37 | + #if report type is "T" then print out the sum total only |
| 38 | + elif report_type.upper() == "T": |
| 39 | + break |
| 40 | + #if not a digit and if not a "Q" then print a message that the "input is invalid" |
| 41 | + else: |
| 42 | + print("Invalid Entry") |
| 43 | + print() |
| 44 | + print("total:") |
| 45 | + return total |
| 46 | +print(adding_report(report_type)) |
0 commit comments