Python Forum
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Assignment
#1
## Program: adding_report() function
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".
The **adding_report()** function has 1 string parameter which indicates the type of report:
- "A" used as the argument to adding_report() results in printing of all of the input integers and the total
- "T" used as the argument results in printing only the total

#### Sample input and output:
call adding_report() with "A" as argument (print all the integers entered and the total)
`
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
`

call with "T"(print only the total)
`
Input an integer to add to the total or "Q" to quit
Enter an integer or "Q": 5
Enter an integer or "Q": 7
Enter an integer or "Q": Quit

Total
12
`

### The forever (while True) loop diagram
This diagram represents only part of the assignment - it is the loop and nested if statements inside the function. The code will enter at the while True loop after initializing variables.

### Additional Details

- initialize total variable which will sum integer values entered
- initialize items variable which will build a string of the integer inputs separated with a *new line character*
- define the adding_report function with one parameter report that will be a string with default of "T"
- inside the function build a forever loop (infinite while loop) and inside the loop complete the following
- use a variable to gather input (integer or "Q")
- check if the input string is a digit (integer) and if it is...
- add input iteger to total
- if report type is "A" add the numeric character(s) to the item string seperated by a new line
- if not a digit, check if the input string is "Q" or starts with a "Q", **if "Q"** then...
- if the report type is "A" print out all the integer items entered and the sum total
- if report type is "T" then print out the sum total only
- break out of while loop to end the function after printing the report ("A" or "T")
- if not a digit and if not a "Q" then print a message that the "input is invalid"

- Call the adding_report function with "A" and then with "T" report parameters
Reply
#2
You need to show us what you have tried so far (posted between code tags). If you receive an error, post it, in it's entirety, between the error tags.  Be as specific as you can when asking questions.
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#3
Problems solved. Thanks all
Reply
#4
Wall
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
Final Project Required Coding Activity
Introduction to Python Unit 1

This activity is based on modules 1 - 4 and is similar to exercises in the Jupyter Notebooks Practice_MOD04_1-6_IntroPy.ipynb and Practice_MOD04_1-7_IntroPy.ipynb which you may have completed as practice.

Some Assignment Requirements
This program requires the use of
while loop
if, elif, else
if,else (nested)
casting of type, between strings and numbers

The program should only use code syntax covered in modules 1 - 4.

The program must result in print output using the numeric input, similar to that shown in the samples displaying "Items" and "Total".
Program: adding_report() function
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".
The adding_report() function has 1 string parameter which indicates the type of report:

"A" used as the argument to adding_report() results in printing of all of the input integers and the total
"T" used as the argument results in printing only the total
Sample input and output:
call adding_report() with "A" as argument (print all the integers entered and the total)

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
call with "T"(print only the total)

Input an integer to add to the total or "Q" to quit
Enter an integer or "Q": 5
Enter an integer or "Q": 7
Enter an integer or "Q": Quit

Total
12
The forever (while True) loop diagram
This diagram represents only part of the assignment - it is the loop and nested if statements inside the function. The code will enter at the while True loop after initializing variables.

image of while True Loop with nested if statements described in bulleted text above

Additional Details
initialize total variable which will sum integer values entered

initialize items variable which will build a string of the integer inputs separated with a new line character

define the adding_report function with one parameter report that will be a string with default of "T"

inside the function build a forever loop (infinite while loop) and inside the loop complete the following

use a variable to gather input (integer or "Q")
check if the input string is a digit (integer) and if it is...
add input iteger to total
if report type is "A" add the numeric character(s) to the item string seperated by a new line
if not a digit, check if the input string is "Q" or starts with a "Q", if "Q" then...
if the report type is "A" print out all the integer items entered and the sum total
if report type is "T" then print out the sum total only
break out of while loop to end the function after printing the report ("A" or "T")
if not a digit and if not a "Q" then print a message that the "input is invalid"
Call the adding_report function with "A" and then with "T" report parameters

Run and test your code before submitting
Reply


Forum Jump:

User Panel Messages

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