# if and else statements

Page 1 of 1

## 8 Replies - 3625 Views - Last Post: 23 February 2010 - 05:41 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=157651&amp;s=431efa017b22bfdb297374af5f755a98&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 j1_osu2002

Reputation: 2
• Posts: 57
• Joined: 09-February 09

# if and else statements

Posted 22 February 2010 - 04:07 PM

Can anyone tell me if I am doing this right. The problem asks me: Write nested decision structures that perform the following: If ammount1 is greater than 10 and amount2 is less than 100, display the greater of amount1 and amount2.

Here is what I have so far.

```#!/usr/bin/env python

amount1 = input('Enter an amount for amount1: ')
amount2 = input('Enter an amount for amount2: ')

if amount1 > 10 and amount2 < 100:
if amount1 > amount2:
print amount1
else:
print amount2
else:
print 'Amounts not within valid range'

```

Any help is greatly appreciated.

Is This A Good Question/Topic? 0

## Replies To: if and else statements

### #2 toshiro

Reputation: 21
• Posts: 137
• Joined: 27-June 09

## Re: if and else statements

Posted 22 February 2010 - 04:58 PM

in Python3, print works like a function:

```print ('Amounts not within valid range')

```

Other than that, your decision nesting is good to go.

### #3 j1_osu2002

Reputation: 2
• Posts: 57
• Joined: 09-February 09

## Re: if and else statements

Posted 22 February 2010 - 05:19 PM

toshiro, on 22 February 2010 - 03:58 PM, said:

in Python3, print works like a function:

```print ('Amounts not within valid range')

```

Other than that, your decision nesting is good to go.

Thanks for the reply. When it prints it only outputs the greater of either amount1 or amount2. It's only supposed to display one of them, correct?

### #4 toshiro

Reputation: 21
• Posts: 137
• Joined: 27-June 09

## Re: if and else statements

Posted 22 February 2010 - 06:27 PM

with your current logic, if amount1 is equal to amount2, it will print out amount2, thats the only weirdness I can see.

### #5 j1_osu2002

Reputation: 2
• Posts: 57
• Joined: 09-February 09

## Re: if and else statements

Posted 23 February 2010 - 03:03 PM

Yea thanks for your help. What I'm gonna do is add == before everything. Here is my final result:
```#!/usr/bin/env python

# prompts user to type in a number for amount1
# amount2
amount1 = input('Enter an amount for amount1: ')
amount2 = input('Enter an amount for amount2: ')

# Determines and prints the greater of amount1 and amount2.
# If amount1 == amount2 then it displays both numbers are equal
# so both are greater
# If amount1 > amount2 then it displays amount1.
# or if amount1 is less than amount2, it prints
# amount2.
if amount1 > 10 and amount2 < 100:
if amount1 == amount2:
print amount1, 'equals', amount2
elif amount1 > amount2:
print '--------------------------------'
print amount1, 'is greater than amount2'
else:
print '--------------------------------'
print amount2, 'is greater than amount1'

# Displays only if the amount1 is less than 10 and
# amount2 is greater than 100
else:
print 'Amounts not within valid range'

```

This post has been edited by j1_osu2002: 23 February 2010 - 03:04 PM

### #6 j1_osu2002

Reputation: 2
• Posts: 57
• Joined: 09-February 09

## Re: if and else statements

Posted 23 February 2010 - 03:21 PM

Yea thanks for your help. What I'm gonna do is add == before everything. Here is my final result:
```#!/usr/bin/env python

# prompts user to type in a number for amount1
# amount2
amount1 = input('Enter an amount for amount1: ')
amount2 = input('Enter an amount for amount2: ')

# Determines and prints the greater of amount1 and amount2.
# If amount1 == amount2 then it displays both numbers are equal
# so both are greater
# If amount1 > amount2 then it displays amount1.
# or if amount1 is less than amount2, it prints
# amount2.
if amount1 > 10 and amount2 < 100:
if amount1 == amount2:
print amount1, 'equals', amount2
elif amount1 > amount2:
print '--------------------------------'
print amount1, 'is greater than amount2'
else:
print '--------------------------------'
print amount2, 'is greater than amount1'

# Displays only if the amount1 is less than 10 and
# amount2 is greater than 100
else:
print 'Amounts not within valid range'

```

### #7 toshiro

Reputation: 21
• Posts: 137
• Joined: 27-June 09

## Re: if and else statements

Posted 23 February 2010 - 03:21 PM

print needs to be a function:
```print('Hello World')

```

### #8 Dogstopper

Reputation: 2915
• Posts: 11,169
• Joined: 15-July 08

## Re: if and else statements

Posted 23 February 2010 - 03:58 PM

toshiro, on 23 February 2010 - 05:21 PM, said:

print needs to be a function:
```print('Hello World')

```

Unless he is using Python 2.x which is a huge possibility. Python 2.6 is *technically* used more at the moment than Python 3.x, but WHEN he switches, yes, the print FUNCTION must be used.

### #9 j1_osu2002

Reputation: 2
• Posts: 57
• Joined: 09-February 09

## Re: if and else statements

Posted 23 February 2010 - 05:41 PM

Dogstopper, on 23 February 2010 - 02:58 PM, said:

toshiro, on 23 February 2010 - 05:21 PM, said:

print needs to be a function:
```print('Hello World')

```

Unless he is using Python 2.x which is a huge possibility. Python 2.6 is *technically* used more at the moment than Python 3.x, but WHEN he switches, yes, the print FUNCTION must be used.

I have Python 3.1 installed on my MacBook Pro and it works fine without the parentheses. I'm using Komodo IDE!