Sep-15-2018, 04:07 PM
Hi^^
I just cannot find my mistake regards to an ecercise I have made.
Before asking my questions, I just include here my Code and the output:
Output:
Any ideas, where I have made a mistake to cause this bug?
other question/concerns are:
1.) How this little program knows whicch number it has defiend as the secretnumber that is to guess?
I see only that within the code a range is defined, of where the secretnumber lies, but I do not see an exact determined value?
2.) Is 'guessesTaken' somehow an already defined value in python?
I just cannot find my mistake regards to an ecercise I have made.
Before asking my questions, I just include here my Code and the output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# This is a guess the number game. import random secretNumber = random.randint( 1 , 20 ) print ( 'I am thinking of a number between 1 and 20.' ) # Ask the player to guess 6 times. for guessesTaken in range ( 1 , 7 ): print ( 'Take a guess.' ) guess = int ( input ()) if guess < secretNumber: print ( 'Your guess is too low.' ) elif guess > secretNumber: print ( 'Your guess is too high.' ) else : break # this condition is the right guess! if guess = = secretNumber: print ( 'Good job! You guessed my number in ' + str (guessesTaken) + 'guesses!' ) else : print ( 'Nope. The number I was thinking of was ' + str (secretNumber)) |
Output:Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:06:47) [MSC v.1914 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>>
RESTART: C:\Users\kr-ga\AppData\Local\Programs\Python\Python37-32\BOOK_automate_the\Chapter 3\guesstheNumber.py
I am thinking of a number between 1 and 20.
Take a guess.
7
Your guess is too low.
Nope. The number I was thinking of was 18
Take a guess.
6
Your guess is too low.
Nope. The number I was thinking of was 18
Take a guess.
As you can see, the code does not loop as it supposeldy should after the user has taken a guess (input), or it does, but not properly - meaning it jumps right away to a solution.Any ideas, where I have made a mistake to cause this bug?
other question/concerns are:
1.) How this little program knows whicch number it has defiend as the secretnumber that is to guess?
I see only that within the code a range is defined, of where the secretnumber lies, but I do not see an exact determined value?
2.) Is 'guessesTaken' somehow an already defined value in python?