NumberGuess issues


#1



When I type python NumberGuess.py into the command line I don't recieve any error messages, but nothing displays either.


from random import randint
from time import sleep
def get_user_guess():
  user_guess = int(raw_input("Please enter 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
  return ("The maximum possible value is:" + str(max_val))
  sleep(1)
  user_guess = get_user_guess()
  if user_guess > max_val:
    print ("Invalid Entry")
    return
  else:
    print ("Rolling...")
    sleep(2)
    print 'The first value is: %d' % (first_roll)
    sleep(1)
    print 'The second value is: %d' % (second_roll)
    sleep(1)
    total_roll = first_roll + second_roll
    print (total_roll)
    print ("Result...")
    sleep(1)
 		if user_guess > total_roll:
      return ("You Won!!!")
    else:
      return ("You Lost")
roll_dice(6)


#2

Hi @awhogue15,

Make sure to click the Save button before you enter ...

python NumberGuess.py

... into the command line.

In your code, you need to make some adjustments to the indentation, and some of the return statements need to be made into print statements. The condition for a win needs to be modified.

First, note that your last line is ...

roll_dice(6)

That line is correct, but since it calls the roll_dice function without displaying the function's return value, the roll_dice function should use print statements to communicate with the user, rather than to return the result of the game. For example, where you have ...

      return ("You Won!!!")

... or ...

      return ("You Lost")

... you should use print instead of return.

Check the indentation of this line ...

 		if user_guess > total_roll:

It should match the indentation of the line above it.

Also note that to win, the user must guess total_roll exactly, rather than to exceed it. Accordingly, the line should be ...

    if user_guess == total_roll:

After making the adjustments to your code, let us know how it works out.


#3

I made all of the adjustments you suggested, but it says that there is an unexpected indent for the line:

if user_guess == total_roll:


#4

It appears that you used spaces to indent all of your lines of code except for that one, in which you may have used tabs instead. If so, use only spaces to indent that line, as well.


#5

Here is my updated code; I used spaces instead of tabs for that line, but now its gone back to not displaying anything.

#This program rolls a pair of dice and asks the user to guess a number, and compares that number to the number generated by the program, and the largest number wins
from random import randint
from time import sleep
def get_user_guess():
  user_guess = int(raw_input("Please enter 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
  return ("The maximum possible value is:" + str(max_val))
  sleep(1)
  user_guess = get_user_guess()
  if user_guess > max_val:
    print ("Invalid Entry")
    return
  else:
    print ("Rolling...")
    sleep(2)
    print 'The first value is: %d' % (first_roll)
    sleep(1)
    print 'The second value is: %d' % (second_roll)
    sleep(1)
    total_roll = first_roll + second_roll
    print (total_roll)
    print ("Result...")
    sleep(1)
    if user_guess == total_roll:
      print ("You Won!!!")
    else:
      print ("You Lost")
roll_dice(6)

#6

This line ...

  return ("The maximum possible value is:" + str(max_val))

... needs to be a print statement instead of a return statement. As a return statement, it terminates execution of the roll_dice function, and the user never gets to see any output..


#7

It works!!!! Thank you so much for your help.


#8