NumberGuess Indentation Error


#1

Can someone find the indentation error? I copied and re-formatted in a text editor and I still keep getting the following error:

SyntaxError: invalid syntax
$ python NumberGuess.py
  File "NumberGuess.py", line 19
    return user_guess
         ^

"""
The program should do the following:

Randomly roll a pair of dice
Add the values of the roll
Ask the user to guess a number
Compare the user's guess to the total value
Decide a winner (the user or the program)
Inform the user who the winner is

"""

from random import randint
from time import sleep

def get_user_guess():
    user_guess =  int(raw_input("Guess a number: ")
    return user_guess

def roll_dice(number_of_sides):
    first_roll = randint(1,number_of_sides)
    second_roll = randint(1,number_of_sides)
    max_val = number_of_sides * 2
    print "The maximum possible value is " + str(max_val)
    time.sleep(1)
    user_guess = get_user_guess()

    if(user_guess > max_val):
        print "No guessing higher than the maximum possible value!"
        return
    else:
        print "Rolling..."
        time.sleep(2)
        print "Your first roll yielded a %d" % (first_roll)
        time.sleep(1)
        print "Your second roll yielded a %d" % (second_roll)
        time.sleep(1)
        total_roll = first_roll + second_roll
        print "Your total roll is %d" % (total_roll)
        print "Result..."
        time.sleep(1)
        if user_guess > total_roll:
            print "You won!"
            return
        else:
            print "You lost, try again!"
            return

roll_dice(6)

#2

from random import randint
from time import sleep

def get_user_guess():
    user_guess =  int(raw_input("Guess a number: ")
    return user_guess

def roll_dice(number_of_sides):
    first_roll = randint(1,number_of_sides)
    second_roll = randint(1,number_of_sides)
    max_val = number_of_sides * 2
    print "The maximum possible value is " + str(max_val)
    time.sleep(1)
    user_guess = get_user_guess()

    if(user_guess > max_val):
        print "No guessing higher than the maximum possible value!"
        return
    else:
        print "Rolling..."
        time.sleep(2)
        print "Your first roll yielded a %d" % (first_roll)
        time.sleep(1)
        print "Your second roll yielded a %d" % (second_roll)
        time.sleep(1)
        total_roll = first_roll + second_roll
        print "Your total roll is %d" % (total_roll)
        print "Result..."
        time.sleep(1)
        if user_guess > total_roll:
            print "You won!"
            return
        else:
            print "You lost, try again!"
            return

roll_dice(6)

#3

Re-posted code below w/proper formatting.


#4

Hi @kokorico,

As this line ends, you have a parenthesis that has not been closed ...

    user_guess =  int(raw_input("Guess a number: ")

#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.