I am trying to write a program that retrieves user input for sales for every weekday and then calculates the total. What I have got so far looks like this.
days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']
index = 0
def sales_by_days(days):
while index < len(days)
sales1 = float(input("Enter sales for", days[0], "here. "))
// then repeat for the rest of the elements //
index = 1
totalsales = sales1 sales2 sales3 sales4 sales5
print ("Your total sales for the week are", totalsales, ". ")`
It's not really having the desired results. Can anybody give me some pointers on where I am going wrong?
CodePudding user response:
You can use for
loop instead of while
loop here,
days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']
def sales_by_days(days):
totalsales = 0
for day in days:
sales = float(input(f"Enter sales for {day} here. "))
totalsales = sales
print(f"Your total sales for the week are {totalsales} .")
Execution:
Enter sales for Monday here. 10
Enter sales for Tuesday here. 2
Enter sales for Wednesday here. 4
Enter sales for Thursday here. 6
Enter sales for Friday here. 8
Your total sales for the week are 30.0 .
CodePudding user response:
If your goal is to receive user input for each day's sales and calculate the sum, you must either add to a global variable total sales on every loop iteration or store each of the sales in a global list variable sales and take its sum afterwards.
# Your solution modified
days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']
index = 0
total_sales = 0
def sales_by_days(days):
while index < len(days):
total_sales = float(input("Enter sales for", days[index], "here. "))
index = 1
print ("Your total sales for the week are", total_sales, ". ")`
OR
# Maybe a cleaner, simpler solution
def sales_by_days():
days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']
total = 0
for day in days:
total = float(input("Enter sales for ", day, " here."))
print("Your total sales for the week are ", total_sales, ".")`
CodePudding user response:
To maintain academic integrity, I have approached the problem in a different manner. However, the process (and learning) should remain similar and consequently this response should be useful.
sales_quantity = {'monday': 0.0, 'tuesday': 0.0, 'wednesday': 0.0, 'thursday': 0.0, 'friday': 0.0, 'saturday': 0.0, 'sunday': 0.0}
for day in sales_quantity.keys():
sales_quantity[day] = round(float(input("Enter the sales quantity for " day ": ")), 2)
for day, quantity in sales_quantity.items():
print(f"The sales quantity for {day} is {str(quantity)}")
total_quantity = sum(sales_quantity.values())
print(f"The total quantity this week is {total_quantity}")
The dictionary sales_quantity
contains an 'attribute' of keys and values. You may invoke the .keys()
or .values()
of any dictionary to return the "dict_keys" or "dict_values" object. This object may be iterated through in an identical manner of a list object. The advantage of using a dictionary over two separate lists, however, is the interlinking of the lists with a single object.
In the for day in sales_quantity.keys()
, you are iterating through the de-facto 'list' (really the type dict_keys). You assign each of the individual instances to the variable day
.
The round(val, 2)
is used to clean the input. Otherwise, a user may input unreasonable values.
The sales_quantity[day] = var
is used to alter the data into the dictionary.
The print(f"
indicates to use the format print method. This allows you to embed variables easily.