Probably as noob a question as they come if, while, elif

Hi all

I’m very new to python, so this may be a basic question but that’s the idea right.

How do I start again or break the loop to continue?

I’m building a text game (i think the tut video was linked from here) to help learn.
I’m expanding on the basic format of the lesson.

It asks the users name
then asks the users age.
if 18+ it continues.

If 14 - 17 it asks for the parents, then asks if they are present, it says they should input 18yrs when they are asked the age question again. **this is where I’d like it to restart the game to ask the age again and when 18 is inputted it would continue like the original question

If under 14, it tells the kid to lie and to type 18 in the age **this is where I’d like it to restart the game to ask the age again and when 18 is inputted it would continue like the original question

The user inputting etc all work fine but the game either stops after the parents are present or if I use while, it loops asking if the parents are present

eg line 20 returns y then line 6 would start again giving chance to input 18 and continue the game

I’ve tried while, break, continue, exit and few others I can’t remember?
I’ve tired swapping the order of if statements,

I am not a Python guy, but this line struck me:

if age >= 14 <= 18:

That makes sense in terms of English:

if age is greater than and equal to 14 and less than or equal to 18

But a computer doesn’t read it that way (at least no language that I’ve ever studied.)

I don’t know Python well enough, but I’m guessing that it would break it apart by resolving age >= 14 first as true or false and then check if that is <=18, which of course is nonsensical so it will (I assume) resolve to false.

How you need to think about this is this:

if age is greater than and equal to 14 and (age is) less than or equal to 18

This, I’m guessing, in Python would be:

if ((age >= 14) and (age <= 18)):

After I read your description and then read your code I asked myself why aren’t you using for loops or while loops? It seems like your code would flow better if you did especially when you describe what you want your program to do you use the word ‘for’. You can still incorporate the if statements too. So I think try using a list with nested loops. If this isn’t any help then I’ll try to show ya. Just let me know :)!

thanks for the reply kevin.
the
if age >= 14 <= 18:

bit actually works fine, although my noob insticts put them in brackets when I first tried.

it’s the returning to the original question I’m struggling with and know it will be something simple… it nalways is

I’ve seen for loops in my search for answers but I don’t know too much on them just yet. I will certainly take another look at them.
Thanks for you’re help

Oh I have tried while loops though, but but kept looping. Ha ha

Also, I’m currently starting the first python project and I’m struggling. I found this site called python principles and its really helping me understand everything more. Especially loops so check it out if you need more help. The site is formatted like what I assume freecodecamp will put out for the python curriculum when they finish it. Since they currently just have Dr.Chuck’s videos, that can be a bit harder to follow through. It certainly is for me so python principles helped me with their interactive lessons.

1 Like

Yeah lol, try for loops. but first define your list then apply the loop a step at a time

I think I understand.
one list would be 0 - 13, another 14 - 17, then say 18 - 100 or could I put >18 instead of 18 - 100
…Perhaps?

Yeah you can nest lists within a list. Then you can access them afterwards. Also I just noticed that in the middle of your code you a space after ‘lower’ and you dont have lower attached to anything. That could also be causing you problems

i should be . lower

This means if the user inputs BiGandSMAll it will convert to lower case

. upper I think does the opposite or converts to upper case should I say

edit: Oh gotcha, it’s missing the () after .lower

. lower ()

would be correct

Thanks

Hi @godsdoctor,

As @kevinSmith pointed, there is something with your first if statement.

My understanding from reading your code is that you 'd like to check whether age is between 14 and 18 (both inclusive). But, later you check whether player’s age is 18 or greater. In theory, you would go through both if's codes when age == 18.

I would take a more mathematical approach on the first if statement.

if 14 <= age < 18:
  # This will read, if age is between 14 and 18 (not including 18)

Now, it should fail if age is 18 or greater, and go through your questions.

Try this on the REPL or on your local python interpreter:

In [12]: age = 19
In [13]: age >= 14 <= 18
Out[13]: True
In [16]: 14 <= age < 18
Out[16]: False
In [17]: (age >= 14) and (age < 18)  # Kevin's suggestion
Out[17]: False

Reasoning through, it works for you when you try 18, because Python reads “if age is greater than or equal to 14, and 14 is less than or equal than 18…” the second conditions evaluates to True always.

Finally, you can mash-up all your conditions together, like:

if 14 <= age < 18:
  # go through your questions
elif age >= 18:
  # you are old enough
else:  
  # cannot play, age is under 14

About using a loop for your game, although I cannot see any on your code, a while True: is often seen in code examples. You can break out from the loop.
continue however will jump to the next iteration of a for or a while loop, skipping the code below continue.

Don’t know if all this would help.

1 Like

Thanks for your reply I appreciate the time for a large answer.

I’m pretty sure I understand what you are saying, it has definitely given me another viewpoint of how to construct my err, ahem!..code.

Cheers

1 Like

I checked out python principles and it was free upgrade. Nice!
I didn’t look much more but joined while they have the offer.

I understand the Dr Chuck reference now. Ha ha.
The video I watched must have been from elsewhere, the guy that helped me make sense of python is a guy called Tech with Tim on youtube

Thanks for the info