Unexpected indent/return is not part of function


#1

I had to indent my if statement inside the roll_dice function because otherwise it says return is not a part of the function but then it says that there is an unexpected indent which also gives me an error

from random import randint
import time

def get_user_input():
  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 highest possible number is " + str(max_val)
 time.sleep(1)
  	user_guess = get_user_guess()   
	if user_guess > max_val:
		print "guess exceeds the highest possibility"
		return
	else:
		print "Rolling..." 
   time.sleep(2)
		print "This roll shows: %d" % first_roll
	 time.sleep(1)
		print "This roll shows: %d" % second_roll
	 time.sleep(1)

#2

Hi @williamburgdorffjens,

It appears that your indentations consist of a combination of spaces and tabs. Use one or the other, and not both. For this project, it would be best to use spaces.

Some of the indentations within blocks are not aligned. For example, the indentation of this statement …

 time.sleep(1)

… is not aligned with this one that immediately precedes it …

  	print"The highest possible number is " + str(max_val)

There are additional statements that need to be realigned.

Go through all of the code to redo the indentation. The default for this project seems to be two spaces for each level of indentation, so it would probably best for you to perform it in that manner.

One level of indentation would be two spaces, and two levels would be four spaces, et cetera.

If you encounter a problem, post your code again so we can look at it.


#3

my indentation was all messed up because I was trying to fix the issue of return not being in roll_dice which still persist after completely redoing the indentation


#4

If the problem still persists, could we see the most recent version of your code, so that we can specifically identify the issue?


#5

error ‘return’ outside function

from random import randint
import time

def get_user_input():
	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 highest possible number is " + str(max_val)
	time.sleep(1)
	user_guess = get_user_guess()   
if user_guess > max_val:
	print "guess exceeds the highest possibility"
	return
else:
	print "Rolling..." 
	time.sleep(2)
	print "This roll shows: %d" % first_roll
	time.sleep(1)
	print "This roll shows: %d" % second_roll
	time.sleep(1)

#6

Portions of the code do not have the indentation that they need. There is also a call to a get_user_guess function that is undefined. Here, you probably intended to call the get_user_input function …

	user_guess = get_user_guess()  

Following is your code with the above statement corrected, with indentation adjusted using only spaces and not tabs, and with a call to the roll_dice function added …

from random import randint
import time

def get_user_input():
	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 highest possible number is " + str(max_val)
  time.sleep(1)
  user_guess = get_user_input()
  if user_guess > max_val:
    print "guess exceeds the highest possibility"
    return
  else:
    print "Rolling..." 
    time.sleep(2)
    print "This roll shows: %d" % first_roll
    time.sleep(1)
    print "This roll shows: %d" % second_roll
    time.sleep(1)
    
roll_dice(6)

The above executes without an error.

The instructions may ask for additional code to be written, depending upon what step you have completed.


#7

thanks alot this was giving me alot of trouble but it finally works


#8

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