Python Forum
'Looping' does not work out within a 'for Loop'
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
'Looping' does not work out within a 'for Loop'
#1
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?
Reply


Messages In This Thread
'Looping' does not work out within a 'for Loop' - by Placebo - Sep-15-2018, 04:07 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Questions about while loop - why does one work, and the other variant does not Swgeek 2 997 Aug-29-2024, 07:57 AM
Last Post: Swgeek
  While Loop Does Not Work Properly mactron 4 2,092 Jun-22-2023, 01:04 AM
Last Post: mactron
  "while" loop is looping back too early mangurian 1 1,928 Jan-28-2022, 09:15 AM
Last Post: ibreeden
  For Loop Works Fine But Append For Pandas Doesn't Work knight2000 2 3,172 Dec-18-2021, 02:38 AM
Last Post: knight2000
  How can this for loop work without : ? Pedroski55 1 2,376 Dec-13-2020, 01:19 AM
Last Post: palladium
  Please help my while loop does not work as expected KingKhan248 6 3,979 Sep-28-2020, 09:12 PM
Last Post: deanhystad
  While loop keeps looping mcoliver88 3 3,193 Jul-29-2020, 12:48 PM
Last Post: buran
  Nested for loop not looping puttingwordstogether 0 2,434 Jun-16-2020, 11:15 PM
Last Post: puttingwordstogether
  while loop will not stop looping TheTechRobo 5 5,676 Apr-20-2020, 01:47 PM
Last Post: TheTechRobo
  For loop in my __init__ doesn't work as expected Jessy 2 3,170 Nov-18-2019, 10:07 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020