Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to decrease input
#1
I'm very new to coding so my apologies if I'm saying any of this wrong. I am trying to write a code for this prompt:

A company has 3 divisions. After inputing the sales for each division across 4 quarters, I want to print the average sales for each division. Below is the code:

divisions = 3
while divisions > 0:
    total = 0
    quarters = 4
    while quarters > 0:
        sales = float(input("Enter sales: "))
        total += sales
        quarters -= 1
    average = total / 4
    divisions -= 1
    print("The average for division", divisions + 1, "is $", format(average, '.2f'))
Here is my problem. When I run the code, it prompts me to "Enter sales". And then after I do it four times, it tells me the average. How can I write this code so that it will ask me to enter the sales for Division 1 Quarter 1, Division 1 Quarter 2, Division 2 Quarter 2 etc. and then it will print "The average sales for Division 1 were...", "The average sales for Division 2 were..."

THANK YOU
buran write Apr-14-2021, 03:16 PM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Reply
#2
Please read BBCode to learn how to publish Python code, output, error messages.

The answer to your question:
sales = float(input(f"Enter sales for division {divisions} in quarter {quarters}: "))
Reply
#3
This is how I would accomplish that goal:

overall_sales = [[0, 0]]

def average (sales_list: list) -> float :
	return sum (sales_list) / len (sales_list)

def input_quartly_sales (divition_number: int) -> list :
	quarterly_sales = []
	for quarter_number in range (1, 5) :
		prompt = f'Enter sales for division {division_number} for the'
		prompt += f' quarter number {quarter_number} : '
		quarterly_sales.append (float (input (prompt)))
	return quarterly_sales

for division_number in range (1, 4) :
	print (f'\nSales for division {division_number}.')
	overall_sales.append (input_quartly_sales  (division_number))

print ()
for division_number in range (1, 4) :
	print (f'The average sales for division {division_number}', end = '')
	print (f' is ${average (overall_sales [division_number]):,.2f}')
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to decrease latency while recording streaming video. unicorn2019 0 1,232 Nov-15-2021, 02:12 PM
Last Post: unicorn2019
  increase and decrease a slice value? KEYS 2 2,053 Nov-10-2020, 11:35 PM
Last Post: KEYS

Forum Jump:

User Panel Messages

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