stumped
stumped

Reputation: 3293

error with elif statement

This is an example in the Python book I'm reading. When I try to run the program there's an error, and when I check the code for the error elif is highlighted in red. I'm programming in Python 2.5.

import random

secret = random.randint(1, 99)
guess = 0
tries = 0

print "It is a number between 1 and 99. I'll give you six tries. "

while guess != secret and tries < 6:
    guess = input("What's your guess? ")
    if guess < secret:
        print "Too low!"
        elif guess > secret:
            print "Too high"
            tries = tries + 1
            if guess == secret:
                print "Correct! You found my secret!"
                else:
                    print "No more guesses! Better luck next time!"
                    print "The secret number was", secret 

Upvotes: 2

Views: 1607

Answers (3)

Felix Rabe
Felix Rabe

Reputation: 4286

Python is indentation sensitive.

import random

secret = random.randint(1, 99)
guess = 0
tries = 0

print "It is a number between 1 and 99. I'll give you six tries. "

while guess != secret and tries < 6:
    guess = input("What's your guess? ")
    if guess < secret:
        print "Too low!"
    elif guess > secret:
        print "Too high"
        tries = tries + 1
    elif guess == secret:
        print "Correct! You found my secret!"
    else:
        print "No more guesses! Better luck next time!"
        print "The secret number was", secret 

Edit: I know there are still bugs in this, such as tries = tries + 1 which should be somewhere else in the code. But this version at least does not give syntax errors.

Upvotes: 5

Marcelo Cantos
Marcelo Cantos

Reputation: 186078

There are several problems with this code.

  1. The elif and if should have the same indentation level.
  2. You only increment tries in the too-high case.
  3. input() returns a string; you should convert it to an integer.
  4. After testing < and >, the == is redundant.
  5. Since <, > and == cover every case, you'll never reach the else:

Here's a reworking of the logic:

while guess != secret and tries < 6:
    guess = int(input("What's your guess? "))
    if guess < secret:
        print "Too low!"
    elif guess > secret:
        print "Too high"
    tries = tries + 1

if guess == secret:
    print "Correct! You found my secret!"
else:
    print "No more guesses! Better luck next time!"
    print "The secret number was", secret 

Upvotes: 1

The problem lies in your indentation.

Instead of:

if foo:
    foobar()
    elif bar:
        barbaz()

It should be:

if foo:
    foobar()
elif bar:
    barbaz()    

Fixed, your code would then look like this (note, I've also fixed your else at the end to work correctly):

import random

secret = random.randint(1, 99)
guess = 0
tries = 0

print "It is a number between 1 and 99. I'll give you six tries. "

while guess != secret:
    if tries < 6:
        guess = input("What's your guess? ")
        if guess < secret:
            print "Too low!"
        elif guess > secret:
            print "Too high"
            tries = tries + 1
        elif guess == secret:
            print "Correct! You found my secret!"
    else:
        print "No more guesses! Better luck next time!"
        print "The secret number was", secret
        break

Upvotes: 1

Related Questions