2 Replies - 698 Views - Last Post: 11 November 2013 - 12:02 PM Rate Topic: -----

#1 firead   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 04-November 13

Touble with if/elif & variables

Posted 04 November 2013 - 12:56 PM

I am trying to create a program that calculates & displays a percentage discount based on the number of items purchases (yes, one of those CS101 type problems).

I keep getting a syntax error on the colon and/or variable name in the first if statement, but I don't understand what I'm doing wrong. I've tried a few changes but keep getting stuck here.

This is what I have:
# define the main function
def main():
   sales = int(input("Enter the number of packages purchased (eg, 25): ")
 
   #compute discount
   if quantity < 9:
      discount = 0
   elif quantity < 20:
      discount = 0.2
      print "Quantity Discount = 20% off"
   elif quantity < 50:
      discount = 0.3
      print "Quantity Discount = 30% off"
   elif quantity < 100:
      discount = 0.4
      print "Quantity Discount = 40% off"
   else:
   discount = 0.5
   print "Quantity Discount = 50% off"

   # compute & display amount of discount & total amount of purchase
   retailPrice = sales*99
   discountAmount = retailPrice * discount
   totalAmount = retailPrice - discountAmount

   #show totals   
   print "Order Value (before discount): ", retailPrice
   print "Total Discount: $", discountAmount
   print "Total Purchase Price: ", totalAmount
      
# call the main function
main()




Is This A Good Question/Topic? 0
  • +

Replies To: Touble with if/elif & variables

#2 andrewsw   User is offline

  • Entwickler
  • member icon

Reputation: 6601
  • View blog
  • Posts: 26,897
  • Joined: 12-December 12

Re: Touble with if/elif & variables

Posted 04 November 2013 - 02:27 PM

So what is the error message?

But on line 6 quantity is not defined anywhere.

You also have an indentation error on line 18, and probably 19.
Was This Post Helpful? 0
  • +
  • -

#3 witeboy724   User is offline

  • D.I.C Head
  • member icon

Reputation: 84
  • View blog
  • Posts: 208
  • Joined: 21-June 12

Re: Touble with if/elif & variables

Posted 11 November 2013 - 12:02 PM

Andrewsw is right about not setting a quantity variable. If you're getting an error with the input on line 3, then it's because you need an extra ) at the end of the line - one to close the input( and one to close the int(

So,
quantity = int(input("Enter the number of packages purchased (eg, 25): "))

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1