8 Replies - 3314 Views - Last Post: 23 February 2010 - 05:41 PM Rate Topic: -----

#1 j1_osu2002  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • 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  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 21
  • View blog
  • 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.
Was This Post Helpful? 1
  • +
  • -

#3 j1_osu2002  Icon User is offline

  • D.I.C Head

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

Re: if and else statements

Posted 22 February 2010 - 05:19 PM

View Posttoshiro, 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?
Was This Post Helpful? 0
  • +
  • -

#4 toshiro  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 21
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#5 j1_osu2002  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#6 j1_osu2002  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • 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'


Was This Post Helpful? 0
  • +
  • -

#7 toshiro  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 21
  • View blog
  • 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')


Was This Post Helpful? 0
  • +
  • -

#8 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2876
  • View blog
  • Posts: 11,051
  • Joined: 15-July 08

Re: if and else statements

Posted 23 February 2010 - 03:58 PM

View Posttoshiro, 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.
Was This Post Helpful? 0
  • +
  • -

#9 j1_osu2002  Icon User is offline

  • D.I.C Head

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

Re: if and else statements

Posted 23 February 2010 - 05:41 PM

View PostDogstopper, on 23 February 2010 - 02:58 PM, said:

View Posttoshiro, 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!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1