Hello World,
I am going through this book 'Automate the Boring Stuff with Python' and came across the secret number game example (example here: https://autbor.com/guessthenumber/). I decided to code my own game but used the if statement instead of the elif and the programme does not run in the loop and proceeds to the last line of code instead. Any thoughts why? Appreciate the time lads and lasses.
I am going through this book 'Automate the Boring Stuff with Python' and came across the secret number game example (example here: https://autbor.com/guessthenumber/). I decided to code my own game but used the if statement instead of the elif and the programme does not run in the loop and proceeds to the last line of code instead. Any thoughts why? Appreciate the time lads and lasses.
# This is a guess the number game. 2 import random 3 secretNumber = random.randint(1, 20) 4 print('I am thinking of a number between 1 and 20.') 5 6 # Ask the player to guess 6 times. 7 for guessesTaken in range(1, 7): 8 print('Take a guess.') 9 guess = int(input()) 10 11 if guess < secretNumber: 12 print('Your guess is too low.') 13 elif guess > secretNumber: 14 print('Your guess is too high.') 15 else: 16 break # This condition is the correct guess! 17 18 if guess == secretNumber: 19 print('Good job! You guessed my number in ' + str(guessesTaken) + ' guesses!') 20 else: 21 print('Nope. The number I was thinking of was ' + str(secretNumber))
buran write Jan-07-2021, 06:18 AM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Don't include line numbers in the text you post.
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Don't include line numbers in the text you post.