Messing up some loops.

This forum is a place to ask questions about the python programming language.
Post Reply
User avatar
Jake
Helper!
Posts: 5
Joined: 24 May 2018, 08:46

Messing up some loops.

Post by Jake » 03 Sep 2018, 20:24

Hey there. Struggling with a bit of code, for the problem Guess A Number. The program tells the user if their guess is higher or lower than the secret number, yet it only does so once. I.e.:

*imagine the number the program chose is 11*
User: -guesses 50-
Program: The number is too high. Please guess again.
User: -guesses 40-
Program: Please guess again.

The program asks for another guess, yes, but the part that tells the user whether their number is too high/too low is missing. Should I use "while" instead of "if" and "elif" ? Sorry for the bad explanation, not quite sure how to word it.

Below is the code of the loop:

Code: Select all

while secret_number != player_guess:
    print("This is guess: " + str(guess))
    print("This is game:" + str(game))
    player_guess = input("Please make a guess: ")
    player_guess = int(player_guess)
    guess = guess +1

    if secret_number < player_guess:
        pass 
        print("guess is too high. try again.")
        player_guess = input("try again: ")
        player_guess = int(player_guess)
        guess = guess +1


    elif secret_number > player_guess:
        pass 
        print("guess is too low. try again.")
        player_guess = input("try again: ")
        player_guess = int(player_guess)
        guess = guess +1

Thanks, Jake
1 x

Tags:

User avatar
Mr. MacKenty
Site Admin
Posts: 88
Joined: 28 Apr 2018, 17:06
Answers: 3

Re: Messing up some loops.

Post by Mr. MacKenty » 03 Sep 2018, 20:51

Hi Jake!

Your code was asking for user input in lots of different places. I reduced to it one.

Code: Select all

#This is where we initialize variables:

import random
secret_number = random.randint(0,100)
player_guess = 0
guess = 0
game = 0 

# this line below should be removed when someone plays the game, as it prints the answer for the secret number!
# however, it helps us  debug our program

print(str(secret_number))


while secret_number != player_guess:
    print("This is guess: " + str(guess))
    print("This is game:" + str(game))
    player_guess = input("Please make a guess: ")
    player_guess = int(player_guess)
    guess = guess +1

# the conditional checking if an answer is to low or to high should be REALLY simple. 
    if secret_number < player_guess:
        print("guess is too high. try again.")
        guess = guess +1

# the conditional checking if an answer is to low or to high should be REALLY simple. 
    elif secret_number > player_guess:
        print("guess is too low. try again.")
        guess = guess +1
The code above should work. Please take note of the following comments:

1. please comment your code. Like, liberally. :-)
2. please don't use the pass declaration unless you have a reason to use it
3. please only ask your user fro input in one place - it makes it easier to manage.

:-)

:grinning:
0 x

User avatar
Jake
Helper!
Posts: 5
Joined: 24 May 2018, 08:46

Re: Messing up some loops.

Post by Jake » 04 Sep 2018, 18:30

Thank you!
And yeah, I'll work on commenting my code more often.
0 x

Post Reply