Python Forum
pythonroom Siblings Task
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
pythonroom Siblings Task
#1
Hello everyone, I'm currently having trouble doing a task on the website 'pythonroom' in the 'Branching' lesson. For those who don't know what pythonroom is, essentially we use it in class and in our off time to increase our knowledge of Python. The task in which I have been having trouble with is this;

Given the number of older and younger siblings, print
  • only child if the person has no siblings
  • oldest child if the person only has younger siblings
  • youngest child if the person only has older siblings
  • middle child if the person has both younger and older siblings
I have tried to do this task;

older = 0
younger = 0

if older == 1 and younger == 0:
           print ("only child")
elif older == 0 and younger == 1:
           print ("only child")
elif older == 1 and younger > 0:
           print ("oldest child")
elif younger == 1 and older > 0:
           print ("youngest child")
elif older > 0 and younger > 0:
           print ("middle child")
elif older == 0 and younger == 0:
           print ("only child")
However the website says I am wrong, with no comment on what I haven't done correctly (it usually tells me for example if I have not defined a variable). If anyone of you have the ability to possibly help me, I'd be forever thankful.

- Rhys
Reply
#2
I would look at your logic. If I have an older sibling, then I have a sibling. If I have a sibling, then I'm not an only child. Also, I can't be the oldest child if I have an older sibling.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
The way you've described the problem is incomplete. The two values aren't enough to determine the desired result you've described (except in the case of an only child). If there is one older and one younger, "the person" isn't clearly one or the other. You could produce a list or some other collection of results though.

(Dec-01-2016, 11:47 PM)Rhysimus Wrote: However the website says I am wrong, with no comment on what I haven't done correctly (it usually tells me for example if I have not defined a variable). If anyone of you have the ability to possibly help me, I'd be forever thankful.
Can you provide a link to the website? The full message(s) you get back? Does it expect your answer via stdout or does it have other requirements? You hard-coded the variables here, is the site supposed to provide them in some way?
Reply
#4
(Dec-02-2016, 02:30 AM)ichabod801 Wrote: I would look at your logic. If I have an older sibling, then I have a sibling. If I have a sibling, then I'm not an only child. Also, I can't be the oldest child if I have an older sibling.

I feel so stupid now aha, thank you. As I was in such a routine of getting the other tasks done, I wasn't using this logic. Using this code, the website said I was right;

older = 0
younger = 0

if older == 0 and younger == 0:
        print ("only child")
elif older == 0 and younger > 0:
        print ("oldest child")
elif younger == 0 and older > 0:
        print ("youngest child")
elif younger > 0 and older > 0:
        print ("middle child")
Thanks again, ichabod801!

- Rhys

(Dec-02-2016, 04:25 PM)micseydel Wrote: The way you've described the problem is incomplete. The two values aren't enough to determine the desired result you've described (except in the case of an only child).  If there is one older and one younger, "the person" isn't clearly one or the other. You could produce a list or some other collection of results though.

(Dec-01-2016, 11:47 PM)Rhysimus Wrote: However the website says I am wrong, with no comment on what I haven't done correctly (it usually tells me for example if I have not defined a variable). If anyone of you have the ability to possibly help me, I'd be forever thankful.
Can you provide a link to the website? The full message(s) you get back? Does it expect your answer via stdout or does it have other requirements? You hard-coded the variables here, is the site supposed to provide them in some way?

Looking at your post and ichabod801's post, I figured that my logic wasn't correct. As stated above, I was in such a routine to get my tasks done that I sort of just began to lose my mind almost. The website is pythonroom.com. Essentially there are lessons in which I have to do to show the teacher that I understand.

To answer your other questions, it only tells me wrong if for example I haven't used str and int correctly, if I have mismatched open or closed parentheses and such. The variables are hard-coded, however I am able to change the numbers accordingly to see my code work on the website.

Thanks again!

- Rhys
Reply
#5
It's easiest to see how these things should be coded by using
a simple truth table:

   

only child if older siblings = False and younger siblings = False
oldest child if older siblings = False and younger siblings = True
youngest child if older siblings = True and younger siblings = False
middle child if older siblings = True and younger siblings = True
Reply
#6
(Dec-02-2016, 09:09 PM)Larz60+ Wrote: It's easiest to see how these things should be coded by using
a simple truth table:



only child if older siblings = False and younger siblings = False
oldest child if older siblings = False and younger siblings = True
youngest child if older siblings = True and younger siblings = False
middle child if older siblings = True and younger siblings = True

Oh yeah! I didn't really look at the logic in this way, as we've only touched truth tables in class with logic gates. Thank you Larz60+!

- Rhys
Reply
#7
It was helpful to work in engineering R & D before becoming a full time programmer.

Needed two cokes instead of one!
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020