0

I am struggling to end my program in Python, all I want is to type q or quit to end the program when done. Here is my code

# This is a guess the number game
import random

guessesTaken = 0

print('Hello! What is your name?')
myName = input()

number = random.randint(1, 20)
print('Well, ' + myName + ', I am thinking of a number between 1 and 20, can     you guess it?')

while guessesTaken < 6:
    print('Take a guess!')
    guess = input()
    guess = int(guess)

    guessesTaken = guessesTaken + 1

    if guess < number:
       print('Your guess is too low')

    if guess > number:
       print('Your guess is too high')

    if guess == number:
       break

if guess == number:
    guessesTaken = str(guessesTaken)
    print('Good job ' + myName + '!You guessed my number in ' + guessesTaken + '  guesses!')

    if guess  != number:
       number = str(number)
       print('Nope. The number I was thinking of was ' + number)

       print('Game Over')
       choice = input('Press Q to Quit')
       if choice == 'q' :
       sys.exit()  

Please tell me where I am going wrong??

1
  • First, you have an indentation problem. Moreover, input won't return until Enter is pressed. Commented Mar 7, 2016 at 16:54

2 Answers 2

2

From what I can tell you have two errors.

First you need to import the sys module for sys.exit() to work.

Second, your indentation is incorrect in two places. Here is the correct code. The comments show where the indentation is incorrect:

# This is a guess the number game
import random
import sys

guessesTaken = 0

print('Hello! What is your name?')
myName = input()

number = random.randint(1, 20)
print('Well, ' + myName + ', I am thinking of a number between 1 and 20, can     you guess it?')

while guessesTaken < 6:
    print('Take a guess!')
    guess = input()
    guess = int(guess)

    guessesTaken = guessesTaken + 1

    if guess < number:
       print('Your guess is too low')

    if guess > number:
       print('Your guess is too high')

    if guess == number:
       break

if guess == number:
    guessesTaken = str(guessesTaken)
    print('Good job ' + myName + '!You guessed my number in ' + guessesTaken + '  guesses!')

if guess  != number:  #indentation was off here
   number = str(number)
   print('Nope. The number I was thinking of was ' + number)

print('Game Over')  #and here
choice = input('Press Q to Quit')
if choice == 'q' :
    sys.exit()  # and here
Sign up to request clarification or add additional context in comments.

1 Comment

No last two conditions should be unindented, else the break directly ends the program.
1

As @MarkyPython noted, you have some indent error, and you need to import sys if you want to use exit. Note that given the code that you have, it is actually bad practice to use sys.exit(). I would instead recommend that you use break. This will exit your while loop. Because you have no code after that, your program will graciously exit.

If you add more code later on, you'll probably want to put your code inside a function. At that point, using return will be the way to go.

Using break or return is better practice because it makes it easier to add features to your program later on and makes the flow of your code better (no abrupt exit 'jump').

print('Hello! What is your name?')
myName = input()

number = random.randint(1, 20)
print('Well, ' + myName + ', I am thinking of a number between 1 and 20, can     you guess it?')

while guessesTaken < 6:
    print('Take a guess!')
    guess = input()
    guess = int(guess)

    guessesTaken = guessesTaken + 1

    if guess < number:
       print('Your guess is too low')

    if guess > number:
       print('Your guess is too high')

    if guess == number:
       break

    if guess == number:
        guessesTaken = str(guessesTaken)
        print('Good job ' + myName + '!You guessed my number in ' + guessesTaken + '  guesses!')

    if guess  != number:
       number = str(number)
       print('Nope. The number I was thinking of was ' + number)

       print('Game Over')
       choice = input('Press Q to Quit')
       if choice == 'q' :
           break # instead of exit

1 Comment

Thank you very much for your help, will try it later. This is my first attempt at Python as you can probably tell :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.