Can't find the indentation error in my code?


#1

Whenever I run my code, I get an indentation error on line 27. In the code editor it looks fine but when I copy the code to the forums I see the error. I can't fix it though. Could someone else try?


"""
This program rolls a pair of virtual dice and then asks the user to input a number. If the inputed number is larger than the sum of the two dice, the user wins. Otherwise, the computer wins.
"""

# Imports
from random import randint
from time import sleep

# Variable
sides =   int(raw_input("How many sides will the die have?"))

# Functions
def get_user_guess():
		user_guess = int(raw_input("Take a guess!"))
		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 highest possible roll is: " + str(max_val)
		sleep(1)
		user_guess = get_user_guess()
		if user_guess > max_val:
				print "That's past the max value!"
    		return
    else:
    		print "Rolling..."
    		sleep(2)
    		print "The first roll is %d" % (first_roll)
    		sleep(1)
    		print "The second roll is %d" % (second_roll)
    		sleep(1)
    		total_roll = first_roll + second_roll
				print total_roll
    		sleep(1)
        if user_guess > total_roll:
          print "You've won!"
          return
        else:
          print "You've lost!"
          return
            
# Code
roll_dice(sides)


#2

Hi @bborgholthaus,

Use either spaces or tabs for indentation in a Python program, but never use both. Following is your code with only spaces used for indentation ....

"""
This program rolls a pair of virtual dice and then asks the user to input a number. If the inputed number is larger than the sum of the two dice, the user wins. Otherwise, the computer wins.
"""

# Imports
from random import randint
from time import sleep

# Variable
sides =   int(raw_input("How many sides will the die have?"))

# Functions
def get_user_guess():
    user_guess = int(raw_input("Take a guess!"))
    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 highest possible roll is: " + str(max_val)
    sleep(1)
    user_guess = get_user_guess()
    if user_guess > max_val:
        print "That's past the max value!"
        return
    else:
        print "Rolling..."
        sleep(2)
        print "The first roll is %d" % (first_roll)
        sleep(1)
        print "The second roll is %d" % (second_roll)
        sleep(1)
        total_roll = first_roll + second_roll
        print total_roll
        sleep(1)
        if user_guess > total_roll:
            print "You've won!"
            return
        else:
            print "You've lost!"
            return
            
# Code
roll_dice(sides)

Reconsider the condition for this if block ...

        if user_guess > total_roll:
            print "You've won!"
            return

The player should win only if the guess is exactly equal to the computer's total roll. Otherwise, the game is too easy.


#3

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