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
#2
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
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#3
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?
Reply
#4
(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!
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#5
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?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  While Loop Does Not Work Properly mactron 4 918 Jun-22-2023, 01:04 AM
Last Post: mactron
  "while" loop is looping back too early mangurian 1 1,280 Jan-28-2022, 09:15 AM
Last Post: ibreeden
  For Loop Works Fine But Append For Pandas Doesn't Work knight2000 2 2,014 Dec-18-2021, 02:38 AM
Last Post: knight2000
  How can this for loop work without : ? Pedroski55 1 1,700 Dec-13-2020, 01:19 AM
Last Post: palladium
  Please help my while loop does not work as expected KingKhan248 6 2,624 Sep-28-2020, 09:12 PM
Last Post: deanhystad
  While loop keeps looping mcoliver88 3 2,204 Jul-29-2020, 12:48 PM
Last Post: buran
  Nested for loop not looping puttingwordstogether 0 1,713 Jun-16-2020, 11:15 PM
Last Post: puttingwordstogether
  while loop will not stop looping TheTechRobo 5 3,723 Apr-20-2020, 01:47 PM
Last Post: TheTechRobo
  For loop in my __init__ doesn't work as expected Jessy 2 2,362 Nov-18-2019, 10:07 AM
Last Post: buran
Question Why does modifying a list in a for loop not seem to work? umut3806 2 2,297 Jul-22-2019, 08:25 PM
Last Post: umut3806

Forum Jump:

User Panel Messages

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