Arthuritease
Arthuritease

Reputation: 27

Getting discounted price for amount of items from user input

I just completed my pre-course assessment.

The project was:

A cupcake costs 6.50. If you purchase more than 6 cupcakes, each of them will cost 5.50 instead, and if you purchase more than 12, each of them will cost 5.00 instead.

Write a program that asks the user how many cupcakes they want to purchase, and display the total amount they have to pay.

My inputs have been:

amt=input("How many cupcakes would you like to but? ")
if amt>=6:
  disc = amt*0.12
elif amt>=12:
  disc=amt*0.24
print(disc)

I can't seem to get the code running. Can anyone please dump it down for me?

Upvotes: 1

Views: 665

Answers (2)

Broken Phone
Broken Phone

Reputation: 41

Python takes input in string format, you need to cast the input into int but in your code, you're comparing a string with int in the conditionals.

amt=int(input("How many cupcakes would you like to but? "))
  if amt>=6:
    disc = amt*0.12
  elif amt>=12:
    disc=amt*0.24
print(disc)

Upvotes: 1

Erling Olsen
Erling Olsen

Reputation: 740

THE COMPLETE SOLUTION TO YOUR PROBLEM IS:

amt=int(input("How many cupcakes would you like to but? "))

if amt <= 6:
    pancake_price_650 = 6.50
    price_inferior_6 = amt * pancake_price_650
    print(price_inferior_6)
    
elif amt > 6:   
    pancake_price_550 = 5.50
    price_superior_6 = amt * pancake_price_550
    print(price_superior_6)
    
elif amt > 12:
    pancake_price_500 = 5.00
    price_superior_12 = amt * pancake_price_500
    print(price_superior_12)
    

You were wrong in constructing the logic of conditions.

Also, you haven't converted the input to a string. Input is always read as a string, in your code you haven't read it as an integer. Convert string input to int using int(), because int() transforms a string into an integer. You need to add int before input, but put it in parentheses.

Upvotes: 1

Related Questions