Python Forum

Full Version: 'Looping' does not work out within a 'for Loop'
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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:

# 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:

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?
Unindent line 18-21 - that will solve your problem.
Read PEP-8 - that will help you to write Pythonic code.

(Sep-15-2018, 04:07 PM)Placebo Wrote: [ -> ]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?

  1. Returned by call to random.randint
  2. Of course not, among many reasons - it's un-Pythonic Tongue
Hey, thanks for the PEP styleguide-link - gonna put it on my kindle and read through it.
Regards to question 1.)
But some exact int-value does not need to be defined i.e how the program can know what the correct guessed number between 1 and 20 is?

Other than this, can maybe someone post a correct input, so that I can copy it and then try to understand it better by toying around a bit?
(Sep-15-2018, 05:53 PM)Placebo Wrote: [ -> ]Hey, thanks for the PEP styleguide-link - gonna put it on my kindle and read through it.
Regards to question 1.)
But some exact int-value does not need to be defined i.e how the program can know what the correct guessed number between 1 and 20 is?
The value does not matter, if quest is neither smaller nor bigger than the secret number - else in the snippet below actually is equivalent to elif guess == secretNumber - but since this is the only remaining option, the condition is redundant
    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!
Thanks for posting the code - I will try it tomorrow.

Regards to 1.) i.e. how the program knows what the secret number is:

I might have just missed completely that due to the fact that the choosen modul in this example is the 'random-modul', the program refers to this modul and randomly determines the value of the secret number without the need of the coder to determine it?
Is this the point here?