Having a hard time with if, elif, and else statements.

Page 1 of 1

11 Replies - 8902 Views - Last Post: 16 July 2012 - 07:01 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=286000&amp;s=44da08d267a76f5ba6a4c4cd3dee626b&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 mike316

• New D.I.C Head

Reputation: 0
• Posts: 13
• Joined: 04-July 12

Having a hard time with if, elif, and else statements.

Posted 16 July 2012 - 05:07 PM

Hi, I'm stuck on this problem in my book. A software company sells a package that retails for \$99. Quantity discounts are given according to the following Quantity 10-19: discount 20%: Quantity 20-49: discount 30%: Quantity 50-99: Discount 40%: 100 or more: discount 50%. Write a program that asks the user to enter the number of packages purchased. The program should then display the amount of the discount (if any) and the total amount of the purchase.

The program I've written does take the number of purchase and prints out the discount. I'm lost on how to return the amount of discount and the total amount of the purchase after the discount. Any help would be much appreciated. Thanks in advance.

```# The program will figure out
# software price with a discount

# The input for getting the number of packages
def main():

packages = input('Enter the number of packages you would like to order: ')

# My if, elif, and else for the number of packages.
# and it prints the discounts for the number of packages
# entered in the "packages" input.

if packages  <= 19 and packages >= 10:
print 'You receive a twenty percent discount. '

elif packages <= 49 and packages >= 20:
print 'You receive a thirty percent discount. '

elif packages <= 99 and packages >= 50:
print 'You recieve a fourty percent discount. '
else:
print 'You recieve a fifty percent discount. '

main()

```

Is This A Good Question/Topic? 0

Replies To: Having a hard time with if, elif, and else statements.

#2 sepp2k

• D.I.C Lover

Reputation: 2311
• Posts: 3,574
• Joined: 21-June 11

Re: Having a hard time with if, elif, and else statements.

Posted 16 July 2012 - 05:26 PM

I'm not sure I understand the question. You know how many packages the user bought, you know how much one package costs and you what the discount is. So just multiply those factors together and you get the total amount he needs to pay.
Was This Post Helpful? 0

#3 mike316

• New D.I.C Head

Reputation: 0
• Posts: 13
• Joined: 04-July 12

Re: Having a hard time with if, elif, and else statements.

Posted 16 July 2012 - 05:45 PM

If the person buys a certain amount he gets a bigger discount, I'm lost on how and where to write the function for each discount. Do I put the function inside each if, elif, and else statement, can I write one function to do that? I'm at a complete loss.
Was This Post Helpful? 0

#4 sepp2k

• D.I.C Lover

Reputation: 2311
• Posts: 3,574
• Joined: 21-June 11

Re: Having a hard time with if, elif, and else statements.

Posted 16 July 2012 - 05:50 PM

You could set a variable called discount to store the relative discount in each if-elif-else block and then use that variable after the ifs to calculate the total discount and the total amount. That way you could also move the print statement after the ifs.
Was This Post Helpful? 0

#5 mike316

• New D.I.C Head

Reputation: 0
• Posts: 13
• Joined: 04-July 12

Re: Having a hard time with if, elif, and else statements.

Posted 16 July 2012 - 06:01 PM

sepp2k, on 16 July 2012 - 05:50 PM, said:

You could set a variable called discount to store the relative discount in each if-elif-else block and then use that variable after the ifs to calculate the total discount and the total amount. That way you could also move the print statement after the ifs.

Thanks sepp2k, I was pondering something like that. I have to go to bed, I'll sleep and it and try it tommorow. Thanks again for the help.
Was This Post Helpful? 0

#6 k3y

• D.I.C Head

Reputation: 36
• Posts: 205
• Joined: 25-February 12

Re: Having a hard time with if, elif, and else statements.

Posted 16 July 2012 - 06:05 PM

Hmm;
```#!/usr/bin/python3

def main():

packages = int(input('Enter the number of packages you would like to order: '))

if packages  <= 19 and packages >= 10:
print ('You receive a twenty percent discount. ')
twenty(packages)

elif packages <= 49 and packages >= 20:
print ('You receive a thirty percent discount. ')
thirty(packages)

elif packages <= 99 and packages >= 50:
print ('You recieve a fourty percent discount. ')
forty(packages)

else:
print 'You recieve a fifty percent discount. '
fifty(packages)

def twenty(value):
total = reg * value * .2
print('\$', total)

def thirty(value):
total = reg * value * .3
print('\$', total)

def forty(value):
total = reg * value * .4
print('\$', total)

def fifty(value):
total = reg * value * .5
print('\$', total)

reg = 99
main()

```

I essentially just created functions that take an argument (in this case the users total amount of packages). For each function I multipied it by the global value of (99) (the price of 1 package) and thon multiplied it by the various discount prices.

This post has been edited by atraub: 16 July 2012 - 08:56 PM
Reason for edit:: Down-rep was an accident

Was This Post Helpful? -1

#7 sepp2k

• D.I.C Lover

Reputation: 2311
• Posts: 3,574
• Joined: 21-June 11

Re: Having a hard time with if, elif, and else statements.

Posted 16 July 2012 - 06:08 PM

That's a lot of repetition though. Creating four functions that are all the same except for one number is a pretty good sign that you're writing smelly code.

Using a variable like I suggested is much cleaner. If you want it to be a function, you can still do it as one function that takes the relative discount as a parameter.

Also: I just noted that there's a mistake in the logic of the if-statements: The case where the user buys less then 10 packages is handled as if he bought more than 100 packages.

This post has been edited by sepp2k: 16 July 2012 - 06:09 PM

Was This Post Helpful? 0

#8 k3y

• D.I.C Head

Reputation: 36
• Posts: 205
• Joined: 25-February 12

Re: Having a hard time with if, elif, and else statements.

Posted 16 July 2012 - 06:16 PM

sepp2k, on 16 July 2012 - 08:08 PM, said:

That's a lot of repetition though. Creating four functions that are all the same except for one number is a pretty good sign that you're writing smelly code.

Using a variable like I suggested is much cleaner. If you want it to be a function, you can still do it as one function that takes the relative discount as a parameter.

Also: I just noted that there's a mistake in the logic of the if-statements: The case where the user buys less then 10 packages is handled as if he bought more than 100 packages.

Like this

```#!/usr/bin/python3

def main():

packages = int(input('Enter the number of packages you would like to order: '))

if packages  <= 19 and packages >= 10:
print ('You receive a twenty percent discount. ')
discount(packages, .2)

elif packages <= 49 and packages >= 20:
print ('You receive a thirty percent discount. ')
discount(packages, .3)

elif packages <= 99 and packages >= 50:
print ('You recieve a fourty percent discount. ')
discount(packages, .4)

else:
print 'You recieve a fifty percent discount. '
discount(packages, .5)

def discount(value, x):
total = reg * value * x
print('\$', total)

reg = 99
main()

```

Still learning to use Dvorak. Forgive me

This post has been edited by atraub: 16 July 2012 - 08:57 PM
Reason for edit:: UpRepped to cancel down rep

Was This Post Helpful? 1

#9 sepp2k

• D.I.C Lover

Reputation: 2311
• Posts: 3,574
• Joined: 21-June 11

Re: Having a hard time with if, elif, and else statements.

Posted 16 July 2012 - 06:23 PM

Yes, except you have an indentation error in there. I'd also move the print statement into the function because that's needless repetition as well.
Was This Post Helpful? 1

#10 k3y

• D.I.C Head

Reputation: 36
• Posts: 205
• Joined: 25-February 12

Re: Having a hard time with if, elif, and else statements.

Posted 16 July 2012 - 06:40 PM

sepp2k, on 16 July 2012 - 08:23 PM, said:

Yes, except you have an indentation error in there. I'd also move the print statement into the function because that's needless repetition as well.

```#!/usr/bin/python3

def main():
packages = int(input('Enter the number of packages you would like to order: '))

if packages  <= 19 and packages >= 10:
discount(packages, .2)
elif packages <= 49 and packages >= 20:
discount(packages, .3)
elif packages <= 99 and packages >= 50:
discount(packages, .4)
else:
discount(packages, .5)

def discount(value, x):
print('You receive a %d discount') % x
total = reg * value * x
print('\$', total)

reg = 99
main()

```

Is this better?
Was This Post Helpful? 0

#11 sepp2k

• D.I.C Lover

Reputation: 2311
• Posts: 3,574
• Joined: 21-June 11

Re: Having a hard time with if, elif, and else statements.

Posted 16 July 2012 - 06:47 PM

Yes, that's fine except that your call to print is a syntax error because you put the closing ) before % x. I'd also multiply x by 100 and slap a % sign after it for more readable output.
Was This Post Helpful? 1

#12 k3y

• D.I.C Head

Reputation: 36
• Posts: 205
• Joined: 25-February 12

Re: Having a hard time with if, elif, and else statements.

Posted 16 July 2012 - 07:01 PM

sepp2k, on 16 July 2012 - 08:47 PM, said:

Yes, that's fine except that your call to print is a syntax error because you put the closing ) before % x. I'd also multiply x by 100 and slap a % sign after it for more readable output.

```#!/usr/bin/python3

def main():
packages = int(input('Enter the number of packages you would like to order: '))

if packages  <= 19 and packages >= 10:
discount(packages, .2)
elif packages <= 49 and packages >= 20:
discount(packages, .3)
elif packages <= 99 and packages >= 50:
discount(packages, .4)
else:
discount(packages, .5)

def discount(value, x):
print('You receive a %d discount' % x)
total = reg * value * x
print('\$', total)

reg = 99
main()

```

Was This Post Helpful? 0

Page 1 of 1

 .related ul{list-style-type:circle;font-size:12px;font-weight:bold;}.related li{margin-bottom:5px;background-position:left 7px!important;margin-left:-35px;}.related h2{font-size:18px;font-weight:bold;}.related a{color:blue;}