Reputation: 249
I wrote typical guess-number game:
import random
secret = random.randint(1, 99)
guess = 0
tries = 0
print("Hey you on board! I am the dreadfull pirat Robert, and I have a
secret!")
print("that is a magic number from 1 to 99. I give you 6 tries.")
while guess != secret & tries < 6:
guess = input()
if guess < secret:
print("You! Son of a Biscuit Eater! It is too little! YOU Scurvy dog!")
elif guess > secret:
print("Yo-ho-ho! It is generous, right? BUT it is still wrong! The
number is too large, Savvy? Shiver me timbers!")
tires = tries + 1
if guess == secret:
print("Enough! You guessed it! Now you know my secret and I can have a peaceful life. Take my ship, and be new captain")
else:
print("You are not lucky enough to live! You do not have ties. But before you walk the plank...")
print("The number was ", secret)
print("Sorry pal! This number became actually you death punishment. Dead men tell no tales! Yo Ho Ho!")
However spyder executes it all without a stop for inputing number by a user and I have got just this output:
Hey you on board! I am the dreadfull pirat Roberth, and I have a secret! that is a magic number from 1 to 99. I give you 6 tries. You are not lucky enough to live! You do not have ties. But before you walk the plank... The number was 56 Sorry pal! This number became actually you death punishment. Dead men tell no tales! Yo Ho Ho!
I tried to call cmd -> spyder and execute it there (by copy-paste) but I got a lot of mistakes like:
print("The number was ", secret) File "", line 1 print("The number was ", secret)
However, executing this code line by (at least all lines with print) line is not a problem.
How can I execute my code interactively, so that user could give a number and then game continues?
Upvotes: 1
Views: 360
Reputation: 2919
Couple of issues with your code, in the tires=tries+1
you've probably made a code typo.
Second, guess reads in a string so you will need to convert guess into an int to do integer comparisons, use something like guess=int(guess)
.
The reason you aren't seeing this is because your condition in the while loop does not execute as true, run guess != secret & tries < 6
in the interpreter and you'll see that the condition is false.
Instead you should use and
as this is a logical operator the &
is a bitwise logical operator (they are not the same).
while guess != secret and tries < 6:
is the appropriate line of code you should substitute.
Upvotes: 1