Newbie question - Programming help

I’m a master student at university. I’m woking on a minigame related to this.

I have a task to program a ‘while loop’ that accepts input form the user that is supposed to work like this:
If the user types “yes” the game starts, if the user types “no”, they gets a message saying “why are you here then?”, and if they answer something else that is different to “yes” or “no” (e.g. maybe), they gets a message that says “i asked you to type yes or no”, and then goes back to the start. Not the best explanation, but I have no experience with coding - and this is supposed to be newb friendly. :smiley:

Start by ignoring the while loop. Write some code that gets input from the user (hint: you can use the input function) and then does a three-way branch depending on whether the input is “yes”, “no”, or anything else.

Let’s start with that: please post it here as a reply within “code fences”:

```
code goes here
```

You can also use ~~~ instead of the backticks.

I mean … I’m completely noob. So far I have done this:

StartGame = ""

StartGame = input("Are you ready to play?\n")


if StartGame != "yes":
  
  if StartGame == "no":
    print("\nWhy are you here then?!")

  elif StartGame != ["no", "yes"]:
    print("\nI asked you to reply with YES or NO. Is that so hard!?")

  elif StartGame == "yes":
    print("\nGame is starting...") #Somehow...

I’m sure I am way off track here, but somehow it gives the right answers to my inputs, but I guess some function needs to be made also? Or maybe I’m doing it all wrong from the start?
I also tried doing it this way but I’m not even sure this is code at all. LOL

start = ""
answer = ""

input("Are you ready to play?\n\n")

def start_game():
  return answer

while start_game() != "yes":
  
  if answer == "no":
    print("\nWhy are you here then?!")
    
  elif answer != ("yes", "no"):
    print("\nI asked you to reply with YES or NO. Is that so hard!?")

Thanks

What you want to accomplish? One can say: “keep asking user input until answer is one of yes or no”. It’s quite easily translatable into Python:

while (answer := input("Are you ready to play: ").lower()) not in ('yes', 'no'):
    print(f"Expected 'yes' or 'no' but you entered {answer!r}")

User can’t get past this loop unless yes or no is entered. After that answer has that value and you can proceed accordingly.

1 Like

Please give questions a better title than ‘question’. For this, ‘Write yes-no imput loop?’ would be an example.

1 Like

I mean … I’m completely noob. So far I have done this:

StartGame = ""

You can throw this away, because you overwirte it immediately on thenext
line.

 StartGame = input("Are you ready to play?\n")
 if StartGame != "yes":
   if StartGame == "no":
     print("\nWhy are you here then?!")
   elif StartGame != ["no", "yes"]:
     print("\nI asked you to reply with YES or NO. Is that so 
     hard!?")
   elif StartGame == "yes":
     print("\nGame is starting...") #Somehow...

Typically we write this kind of if/elif/…/else chain with
“positive” tests: StartGame == "yes", StartGame == "no" etc rather
than negative tests like StartGame != "yes". They’re both valid, but
the positive ones often seem easier to reason about: if “yes” run the
game, if “no” ask why they’re here, otherwise (else) complain and ask
again.

Look at the tests above. Within the indented stuff you know that
StartGame is not "yes", so the last test can never be true.

Similarly, by the third test StartGame != ["no", "yes"] you already
know that StartGame!="yes" (from the first if-statement) and also that
StartGame!="yes" because you’re in the “false” branch if the second
if-statement. So this test is redundant - it must be true.

However, it will not be true because it isn’t written correctly. You’ve
written:

   elif StartGame != ["no", "yes"]:

The != operator tests for “not equal to”. So it is comparing a string
(StartGame) for equality with a list (the list of values "no" and
"yes"). They’re never going to be equal.

You want a membership test (is StartGame equal to one of the member of
this list), which is written like this:

   elif StartGame not in ["no", "yes"]:

I’m sure I am way off track here, but somehow it gives the right
answers to my inputs,

Are you sure. You’ve tried each of yes, no and something else?

but I guess some function needs to be made also? Or maybe I’m doing it
all wrong from the start?
I also tried doing it this way but I’m not even sure this is code at all. LOL

Is it accepted without a SyntaxError? Then it’s code! It might not be
doing what you want of course…

Let’s have a look:

 start = ""
 answer = ""
 input("Are you ready to play?\n\n")

This runs the input() function, but does not assign its value to
anything. That means that you can’t test that value to see what the user
entered. You want:

 answer = input("Are you ready to play?\n\n")

This function:

 def start_game():
   return svar

returns svar, which you never assign to.

 while start_game() != "yes":

This is good. It says to run a loop until the return value from the
start_game() function is "yes". The logic _consequence of that is
that the start_game() function needs to ask and return an answer. So
you really want it to look like this:

 def start_game():
   svar = input("Are you ready to play?\n\n")
   return svar

Back to your loop:

 while start_game() != "yes":
   if answer == "no":
     print("\nWhy are you here then?!")
   elif answer != ("yes", "no"):
     print("\nI asked you to reply with YES or NO. Is that so hard!?")

The first test is also good. You’re looking at the answer (which we know
at this point to not be "yes") to see if it is "no" and querying the
user’s life choices.

The second test has the same issue as your earlier !=["yes", "no"]
test: the syntax is wrong for what you’re trying to test, and needs to
be:

   elif answer not in ("yes", "no"):
     print("\nI asked you to reply with YES or NO. Is that so hard!?")

That said, by the time you get there you already know that is true
because you’re tested for "yes" in the while-loop condition and you’ve
tested for "no" in the if-statement. So you don’t need to run this
test, and can just write:

   else:
     print("\nI asked you to reply with YES or NO. Is that so hard!?")

And now you’re good to go: the code blocks intil it gets a "yes". Once
that happens it falls out of the while loop. The code for the game
itself would then go below the while-loop.

Cheers,
Cameron Simpson cs@cskk.id.au

Pardon me: they’re never going to be equal because they are different types of things (a string and a list), so the test will always be true. So it will appear that this test is behaving correctly.

Sorry about this misstatement.

Cheers,
Cameron

2 Likes

Thank you so much for the reply. I’ve read it a million times and I thought I understood it. But when I assembled the code it doesn’t work.

May I ask you to write and post a working code so I can run it and study it?

The super small version is (untested):

while True:
    answer = input("play a  game? (yes or no) ")
    if answer == "yes":
        break
    elif answer == "no":
        print("I will ask again")
    else:
        print("I need a yes or a no")

The loop runs forever (True).

All the tests are “positive” i.e. for a specific recognised value rather than “negative” for “not in the recognised values”.

The “yes” runs a break, which exits the loop.

The other recognised response, "no", gets its special print().

Any other response (the else part) gets the “unrecognised” print().

That’s the core logic here. Your game would commence below the while-loop.

Cheers,
Cameron

Haha, yes there it is. Thank you so much, Cameron. Really appreciate you taking your time to even explain in detail why things are working and why they are not.

In my head it seemed so easy, and it really is too, but it’s all the little things you need to remember or else it’s not working at all. Like the input before ("play a game? (yes or no) ").

Again, thanks!