1

This is my first post ever about programming! Folks, when i execute this algorithm (a guessing game), it doesnt stop asking me for more inputs, even when i write "quit", that was supposed to be the quiting word. The "break" order doesnt work, and i can't find out why. Maybe it works, but when it quits the loop, it executes the "startgame()" at the bottom, but i need this "startgame()" at the bottom to make the game run for the first time, since the game is inside a function and i need to call it to start the game.

import random

def startgame():
    a = random.randint (1,10)
    cont = 0

    while True:
        b = input("Guess a number: ")
        if b == 'quit':
            break
        elif int(b) > a:
            print("Too high!")
            cont += 1
            True
        elif int(b) < a:
            print ("Too low!")
            cont += 1
            True
        elif int(b) == a:
            print ("You got it right!")
            print ('You needed ',cont,'guesses!')
            startgame()

startgame()

Any ideas about how to solve this?

0

3 Answers 3

2

Your code has a few small issues, but it actually works, you just didn't test enough to find your actual problem.

The issues:

  • the True lines don't do anything, you don't need them for the while loop; the while will evaluate the True right after it and loop forever.
  • you're mixing single and double quotes for your strings - that gets hard to read and confusing quickly, so you're better off using one style, preferably whatever PEP8 recommends. https://www.python.org/dev/peps/pep-0008/

The problem:

  • your break works just fine; try running your script and entering 'quit' at the first prompt, it quits as expected.
  • the reason it appears not to work is because you restart the entire game by calling the startgame function again after winning the game. This causes a new, nested call from within the game (think 'Inception') and when the game breaks, it ends up on the previous level.

A solution would be to remove the call to startgame() and instead wrapping the whole thing in a second while, for example like this:

import random


def startgame():
    b = 0
    while b != 'quit':
        a = random.randint(1, 10)
        cont = 0

        while True:
            b = input('Guess a number: ')
            if b == 'quit':
                break
            elif int(b) > a:
                print('Too high!')
                cont += 1
            elif int(b) < a:
                print('Too low!')
                cont += 1
            elif int(b) == a:
                print('You got it right!')
                print('You needed ', cont, 'guesses!')


startgame()
Sign up to request clarification or add additional context in comments.

4 Comments

This code fails in Python 2. I believe it is the same problem as the original poster. The result is TypeError: int() argument must be a string or a number, not 'Quitter'.
I agree the startgame call needs to be removed.
When i execute this code, i keep geting the same random number. Something is not right!
@AlexTaylor that problem does not apply to this script. The script works on Python 2 as expected. The message seems to suggest you're running code from the quitter module from "Programming Python, 3rd Edition." @AlexTaylor the startgame() call is not the problem; but you can remove it, if you remove the code from the function. @RafaelSouza the default for PYTHONHASHSEED is to be disabled in Python 2. This causes the random function to always start with the same value. May vary depending on the distribution. Provide a seed yourself (e.g. timestamp) to get a new sequence every time.
0

For times like these, I usually use a simple control variable and run the loop on it. For ex:

right_guess=False
while not right_guess:

and then just go

if guess=right:
    right_guess=True
    break 

Comments

0

just remove the startgame() from inside the loop and replace it by break

import random

def startgame():

    a = random.randint (1,10)
    cont = 0

    while True:
        b = input("Guess a number: ")
        if b == 'quit':
            break
        elif int(b) > a:
            print("Too high!")
            cont += 1

        elif int(b) < a:
            print ("Too low!")
            cont += 1

       elif int(b) == a:
            print ("You got it right!")
            print ('You needed ',cont,'guesses!')
            break # remove this break statement if you want to restart it again after winning  

startgame ()

2 Comments

This helps somewhat, but changes the flow of the game, where it no longer restarts after winning.
if he wants to restart it after winning, means he can remove the break statement

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.