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