Jump to content

if greater than but less than

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
15 replies to this topic

#1
opencircles

opencircles

    Newbie

  • Members
  • Pip
  • 8 posts
i hate not figuring out on my own but im lost

a = input("please enter your age: ")


if a >= 18:

    print"you may enter"

else:

    print"you are to young."

if a > 110:

    print "invalid age entered"



raw_input()

obviously if anythin over 110 is input then it says ivalid age entered but also says you may enter. how do i write if a >= 18 but < 110?

Edited by Jaan, 06 October 2009 - 02:32 AM.
Please use code tags when you are posting your codes !


#2
Hot_Milo23

Hot_Milo23

    Programmer

  • Members
  • PipPipPipPip
  • 120 posts
Posted via CodeCall Mobile
i cant check this atm, but off the top of my head, you could try:
if a>18 && a<110....
or it could be only one &.
hope that helps.

#3
opencircles

opencircles

    Newbie

  • Members
  • Pip
  • 8 posts
wut does the & stand for in python?

and think for the post i will try it in th a.m.

#4
Hot_Milo23

Hot_Milo23

    Programmer

  • Members
  • PipPipPipPip
  • 120 posts
Posted via CodeCall mobi dnt quote me, but it basicly means "and"?

#5
opencircles

opencircles

    Newbie

  • Members
  • Pip
  • 8 posts
ok thank you

#6
TkTech

TkTech

    The Crazy One

  • Moderators
  • 1,396 posts
You're getting confused with C. In C, && is a logical and, whereas a single & is a bitwise and.

In python, its just 'and', so:

if (a >= 18) and (a <= 110):
   print "Zomg!"


#7
Hot_Milo23

Hot_Milo23

    Programmer

  • Members
  • PipPipPipPip
  • 120 posts
haha, of course.
oops.
thx :D

#8
Vswe

Vswe

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 9,552 posts
You should do it like this instead(using an elif block):

 
a = [COLOR=#991616][U]input[/U][/COLOR]("please enter your age: ")
 
if a > 110:
    print "invalid age entered"
elif a >= 18:
    print "you may enter"enter"
else:
    print "you are to young."
 
 
 
raw_input()


#9
UnknownFear

UnknownFear

    Learning Programmer

  • Members
  • PipPipPip
  • 47 posts
@Vswe: But take out the extra "enter"

a = [COLOR=#991616][U]input[/U][/COLOR]("please enter your age: ")
 
if a > 110:
    print "invalid age entered"
elif a >= 18:
    print "you may enter"
else:
    print "you are to young."

raw_input()


#10
Vswe

Vswe

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 9,552 posts
yes of course. A typing error while moving them around with copy/paste. :D

#11
UnknownFear

UnknownFear

    Learning Programmer

  • Members
  • PipPipPip
  • 47 posts
For sure ^^

#12
chili5

chili5

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 7,247 posts

TkTech said:

You're getting confused with C. In C, && is a logical and, whereas a single & is a bitwise and.

In python, its just 'and', so:


if (a >= 18) and (a <= 110):

   print "Zomg!"


You can use a single & on boolean expressions. It is only bitwise and when applied to non boolean expressions. The big difference is that && will stop testing conditions as soon as one condition evaluates to false. Using a single & doesn't stop evaluating conditions.

UnknownFear said:

@Vswe: But take out the extra "enter"

White space doesn't matter.