user14245584
user14245584

Reputation:

add input user values from while loop

I´m a complete beginner and doing an assignment. The different weights for the parcel have different prices, the user has to be able to input the amount of parcels and their individual weight and the program has to tell the total price of all the parcels. This is what i´ve got so far,(i don´t know what but something seems off) i can´t seem to figure out how to make the total price the sum of all the different prices of the parcels (p.s not allowed to use for-loops). Appreciate any help or advice and thanks in advance.

parcel = int(input("How many parcels do you want to send?"))
i = 1
while i < parcel+1:
  print("State the weight for parcel",i,":")
  weight = int(input())
  if (i == parcel):
    break
  i += 1
if weight < 2:
    price = 30
elif weight == 2 or weight < 6:
    price = 28
elif weight == 6 or weight < 12:
    price = 25
elif weight >= 12:
    price = 23
tot_price = 
print("The total price will be:",tot_price)

Upvotes: 0

Views: 113

Answers (1)

haydso
haydso

Reputation: 36

# All prices of each parcel
list_of_prices = []

# Ask the user to input the number of parcels
parcel_count = int(input("How many parcels do you want to send? "))

count = 1

# Loop through the number of parcels, asking for the weight of each
while count <= parcel_count+1:
    weight = int(input(f"Please input the weight for parcel {count}: "))

    if weight < 2:
        price = 30
    elif 2 <= weight < 6:
        price = 28
    elif 6 <= weight < 12:
        price = 25
    elif weight >= 12:
        price = 23

    list_of_prices.append(int(price))      # add the price to the list

    count += 1

# sum() adds up all the numbers in the list of prices
total_price = sum(list_of_prices)
print("The total price will be:", total_price)

Upvotes: 1

Related Questions