webdevRefinery Forum: Another issue with if, else, elif - webdevRefinery Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

User is offline JCJeff 

  • Group: Members
  • Posts: 55
  • Joined: 18-April 12
  • LocationCanada
  • Expertise:Python

Posted 12 May 2012 - 01:14 PM (#1)

Another issue with if, else, elif


Sorry, if I post here too much but I have yet another problem.

The question I want to solve is:
1. A soccer team is looking for girls from ages 10 to 12 to play on their team. Write a program
to ask the user’s age and if male or female (using “m” or “f”). Display a message
indicating whether the person is eligible to play on the team.

2. Bonus: Make the program so that it doesn’t ask for the age unless the user is a girl.

Can someone help me? I want to answer both separately
age = raw_input("How old are you?")
if age == 10 or 11 or 12:
    gender = raw_input("Are you a boy or girl?")
    if gender == "boy" or "girl":
        print "you are eligible to play!"
if age != 10 or 11 or 12:
    print "sorry,you cannot play."

0


User is offline Fike 

  • Group: Members
  • Posts: 340
  • Joined: 26-October 10
  • LocationIreland
  • Expertise:PHP,Javascript,Python,SQL

Posted 12 May 2012 - 01:31 PM (#2)

Not entirely sure what your aim is (could you clarify your post?) however based on what I gather from your post, this might be what you're looking for.

age_dict = [10, 11, 12]

age = raw_input("What is your age? ")
gender = raw_input("Are you a boy or girl? ")

if age in age_dict and gender == "boy" or gender == "girl":
    print("You are eligible to play!")
else:
    print("You are not eligible to play. Sorry about that!")

web developer :: HTML, CSS, JavaScript (node), Python, PHP, MySQL, Mongo.
server admin :: experience with debian (and debian based distros), Gentoo, FreeBSD, OpenBSD.
social :: @nixhead (Twitter), Fudge (IRC), Github (FionnK), Personal Blog.
0


User is offline Sephern 

  • Group: Moderators
  • Posts: 967
  • Joined: 04-June 10
  • LocationReading, UK
  • Expertise:HTML,CSS,PHP,Javascript,Python

Posted 12 May 2012 - 01:33 PM (#3)

View PostJCJeff, on 12 May 2012 - 01:14 PM, said:

Sorry, if I post here too much but I have yet another problem.

The question I want to solve is:
1. A soccer team is looking for girls from ages 10 to 12 to play on their team. Write a program
to ask the user’s age and if male or female (using “m” or “f”). Display a message
indicating whether the person is eligible to play on the team.

2. Bonus: Make the program so that it doesn’t ask for the age unless the user is a girl.

Can someone help me? I want to answer both separately
age = raw_input("How old are you?")
if age == 10 or 11 or 12:
    gender = raw_input("Are you a boy or girl?")
    if gender == "boy" or "girl":
        print "you are eligible to play!"
if age != 10 or 11 or 12:
    print "sorry,you cannot play."


Firstly, in if statements when you separate them with 'or'/'and' then you have to write an entire different clause. I know this is slightly unintuitive (especially in Python) because its different from natural language, but it'd be something like
if age == 10 or age == 11 or age == 12

A more concise way to do it though would be to say
if age in range(10, 13):


Secondly, if you only want to make it ask if the user is male then you'd want to ask that first.

gender = raw_input("Are you a boy or girl? ")
if gender == "boy":
  print "You are eligible to play"
elif gender == "girl":
  age = raw_input("How old are you? ")
  if age in range(10, 13):
    print "You are eligible to play"
  else:
    print "You can't play. Sorry!"
else:
  print "You can't play. Sorry!"


An even better way to do this (where you aren't repeating yourself quite so much) would be to have a function (something like canPlay) which when called, asks the user the relevant questions and returns true (if they can play) and false (if they can't). You can then just call that function and print depending on what it returns. The advantage of this is that if you want to change your messages you only have to do it in one place instead of multiple (the disadvantage being, of course, that you can't have a different error message for girls to boys or whatever).
0


User is offline NeilHanlon 

  • Group: Members
  • Posts: 884
  • Joined: 08-July 10
  • LocationRowley, Massachusetts
  • Expertise:HTML,CSS,PHP,Java,Graphics

Posted 12 May 2012 - 01:42 PM (#4)

Dunno what y'all are smoking, but the directions say that only girls between 10 and 12 can play. Boys can't play at all.
Thanks,
兄ニール

Website | Blog | @NeilHanlon | About.Me | Facebook | LinkedIn
0


User is offline Fike 

  • Group: Members
  • Posts: 340
  • Joined: 26-October 10
  • LocationIreland
  • Expertise:PHP,Javascript,Python,SQL

Posted 12 May 2012 - 01:46 PM (#5)

View PostNeilHanlon, on 12 May 2012 - 01:42 PM, said:

Dunno what y'all are smoking, but the directions say that only girls between 10 and 12 can play. Boys can't play at all.


That's why I told him to clarify. It states that in the first bullet point, but take a look at his code.
web developer :: HTML, CSS, JavaScript (node), Python, PHP, MySQL, Mongo.
server admin :: experience with debian (and debian based distros), Gentoo, FreeBSD, OpenBSD.
social :: @nixhead (Twitter), Fudge (IRC), Github (FionnK), Personal Blog.
0


User is offline TheEmpty 

  • I say words in sequences.
  • Group: Members
  • Posts: 5154
  • Joined: 02-October 10
  • Expertise:HTML,CSS,PHP,Java,Javascript,Python,Ruby on Rails,SQL

Posted 12 May 2012 - 01:46 PM (#6)

View PostNeilHanlon, on 12 May 2012 - 01:42 PM, said:

Dunno what y'all are smoking, but the directions say that only girls between 10 and 12 can play. Boys can't play at all.

His teacher must be a pedo.
Reserved.
1


User is offline Sephern 

  • Group: Moderators
  • Posts: 967
  • Joined: 04-June 10
  • LocationReading, UK
  • Expertise:HTML,CSS,PHP,Javascript,Python

Posted 12 May 2012 - 02:19 PM (#7)

View PostFike, on 12 May 2012 - 01:46 PM, said:

That's why I told him to clarify. It states that in the first bullet point, but take a look at his code.

Ahh missed the word 'girl' in his first point. Should be enough for him to work with anyway though.
0


User is offline JCJeff 

  • Group: Members
  • Posts: 55
  • Joined: 18-April 12
  • LocationCanada
  • Expertise:Python

Posted 12 May 2012 - 03:39 PM (#8)

Okay, I cleaned it up a bit:

age = raw_input("How old are you? ")
gender = raw_input("Are you a boy or girl? ")
if age == 10 or 11 or 12:
    print "You are a", gender, "that is", age, "years old."
    print "you are eligible to play!"
else:
    print "sorry, you cannot play."
'
If you are 10-12 years old, then you can play. If you are not, you cannot

It should be this right?

age = raw_input("How old are you? ")
gender = raw_input("Are you a boy or girl? ")
if age == 10 or age == 11 or age == 12:
    print "You are a", gender, "that is", age, "years old."
    print "you are eligible to play!"
else:
    print "sorry, you cannot play."

0


User is offline Fike 

  • Group: Members
  • Posts: 340
  • Joined: 26-October 10
  • LocationIreland
  • Expertise:PHP,Javascript,Python,SQL

Posted 12 May 2012 - 03:45 PM (#9)

Discard this post.

This post has been edited by Fike: 12 May 2012 - 03:49 PM

web developer :: HTML, CSS, JavaScript (node), Python, PHP, MySQL, Mongo.
server admin :: experience with debian (and debian based distros), Gentoo, FreeBSD, OpenBSD.
social :: @nixhead (Twitter), Fudge (IRC), Github (FionnK), Personal Blog.
0


User is offline JCJeff 

  • Group: Members
  • Posts: 55
  • Joined: 18-April 12
  • LocationCanada
  • Expertise:Python

Posted 12 May 2012 - 03:50 PM (#10)

View PostFike, on 12 May 2012 - 03:45 PM, said:

Discard this post.

age = raw_input("How old are you? ")
gender = raw_input("Are you a boy or girl? ")
if age == 10 or age == 11 or age == 12:
    print "You are a", gender, "that is", age, "years old."
    print "you are eligible to play!"
else:
    print "sorry, you cannot play."

That one works... You have to put age == after each or
0


User is offline Fike 

  • Group: Members
  • Posts: 340
  • Joined: 26-October 10
  • LocationIreland
  • Expertise:PHP,Javascript,Python,SQL

Posted 12 May 2012 - 03:53 PM (#11)

View PostJCJeff, on 12 May 2012 - 03:50 PM, said:

age = raw_input("How old are you? ")
gender = raw_input("Are you a boy or girl? ")
if age == 10 or age == 11 or age == 12:
    print "You are a", gender, "that is", age, "years old."
    print "you are eligible to play!"
else:
    print "sorry, you cannot play."

That one works... You have to put age == after each or


Yeah, I was going to clean up and optimize the code a bit (I'm obsessed with clean code O_o).
web developer :: HTML, CSS, JavaScript (node), Python, PHP, MySQL, Mongo.
server admin :: experience with debian (and debian based distros), Gentoo, FreeBSD, OpenBSD.
social :: @nixhead (Twitter), Fudge (IRC), Github (FionnK), Personal Blog.
0


User is offline JCJeff 

  • Group: Members
  • Posts: 55
  • Joined: 18-April 12
  • LocationCanada
  • Expertise:Python

Posted 12 May 2012 - 03:55 PM (#12)

This is right for the bonus?
gender = raw_input("Are you a boy or girl?")
if gender == "girl":
    age = raw_input("How old are you?")
    if age == 10 or age == 11 or age == 12:
        print "you are eligible to play!"
    else:
        print "sorry, you cannot play."
elif gender == "boy":
    print "you are eligible to play!"
else:
    print "sorry, you cannot play."


Show me your "clean" code please :)
0


User is offline Fike 

  • Group: Members
  • Posts: 340
  • Joined: 26-October 10
  • LocationIreland
  • Expertise:PHP,Javascript,Python,SQL

Posted 12 May 2012 - 03:58 PM (#13)

View PostJCJeff, on 12 May 2012 - 03:55 PM, said:

Show me your "clean" code please :)


Something like

age = raw_input("How old are you? ")
gender = raw_input("Are you a boy or girl? ")

if age in range(10, 13):
    print("You are a %s that is $s years old. You are eligible to play!" % (gender, age))
else:
    print("Sorry, you cannot play.")


Haven't tried it though.
web developer :: HTML, CSS, JavaScript (node), Python, PHP, MySQL, Mongo.
server admin :: experience with debian (and debian based distros), Gentoo, FreeBSD, OpenBSD.
social :: @nixhead (Twitter), Fudge (IRC), Github (FionnK), Personal Blog.
0


User is offline JCJeff 

  • Group: Members
  • Posts: 55
  • Joined: 18-April 12
  • LocationCanada
  • Expertise:Python

Posted 12 May 2012 - 04:00 PM (#14)

View PostFike, on 12 May 2012 - 03:58 PM, said:

Something like

age = raw_input("How old are you? ")
gender = raw_input("Are you a boy or girl? ")

if age in range(10, 13):
    print("You are a %s that is $s years old. You are eligible to play!" % (gender, age))
else:
    print("Sorry, you cannot play.")


Haven't tried it though.

Is it right though? The bonus
0


User is offline Fike 

  • Group: Members
  • Posts: 340
  • Joined: 26-October 10
  • LocationIreland
  • Expertise:PHP,Javascript,Python,SQL

Posted 12 May 2012 - 04:05 PM (#15)

View PostJCJeff, on 12 May 2012 - 04:00 PM, said:

Is it right though? The bonus


Yep, works the way you intended it to on my side.
web developer :: HTML, CSS, JavaScript (node), Python, PHP, MySQL, Mongo.
server admin :: experience with debian (and debian based distros), Gentoo, FreeBSD, OpenBSD.
social :: @nixhead (Twitter), Fudge (IRC), Github (FionnK), Personal Blog.
0


User is offline Quinn 

  • More pew-pew, less QQ
  • Group: Members
  • Posts: 1307
  • Joined: 08-March 10
  • LocationPalmyra, PA, USA
  • Expertise:HTML,PHP,Javascript

Posted 12 May 2012 - 04:29 PM (#16)

Based on your first post, this is what I see:
gender = raw_input("Are you a male or a female (m/f)? ")
if gender == "f" :
	age = raw_input("How old are you? ")
	if int(age) in range(10, 13):
		print "You are eligable to play!"
	else:
		print "You are either too old or too young to play :("
else:
	print "You must be a girl to play."
This will only allow girls between the ages of 10 and 12 exclusive to play.

If you really wanted, you could do this:
genders = ["female", "f", "woman", "girl"]
gender = raw_input("Are you a male or a female? ")
if gender in genders :

Also, do note the
int(age)
bit -
raw_input()
returns a string (at least in 2.7.1).
<Imp> [F3ar 40]  [PWNbear 17]  [magik 15]  [dissident 10]  [mark 7]

View PostKyek, on 07 February 2011 - 07:11 AM, said:

Though anyone who thinks Europe is a country should be smacked in the face. By a train.
0


User is offline NeilHanlon 

  • Group: Members
  • Posts: 884
  • Joined: 08-July 10
  • LocationRowley, Massachusetts
  • Expertise:HTML,CSS,PHP,Java,Graphics

Posted 12 May 2012 - 04:29 PM (#17)

No, the bonus is not correct:

gender = raw_input("Are you a boy or girl? ")
if gender == "f":
    age = raw_input("How old are you? ")
    if age in range(10, 13):
        print("You are a %s that is $s years old. You are eligible to play!" % (gender, age))
    else:
        print("Sorry, you cannot play.")
else:
    print("You are a boy, and apparently it doesn't matter how old you are.")

Thanks,
兄ニール

Website | Blog | @NeilHanlon | About.Me | Facebook | LinkedIn
0


Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

2 User(s) are reading this topic
0 members, 2 guests, 0 anonymous users


Enter your sign in name and password


Sign in options
  Or sign in with these services