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

Page 1 of 1

## 11 Replies - 8825 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=547d5c17e94b289718b4a99710ae4610&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 mike316

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: 2298
• Posts: 3,557
• 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.

### #3 mike316

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.

### #4 sepp2k

• D.I.C Lover

Reputation: 2298
• Posts: 3,557
• 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.

### #5 mike316

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.

### #6 k3y

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

### #7 sepp2k

• D.I.C Lover

Reputation: 2298
• Posts: 3,557
• 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

### #8 k3y

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

### #9 sepp2k

• D.I.C Lover

Reputation: 2298
• Posts: 3,557
• 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.

### #10 k3y

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?

### #11 sepp2k

• D.I.C Lover

Reputation: 2298
• Posts: 3,557
• 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.

### #12 k3y

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()

```