How to make it work


#1


The program tells me the max number is 12 because the max # of sides is 6. It gets down to the user_guess variable which pops you up to the get_user_guess() function which prompts the question. You input the response and it stores the variable...here is where I need help...how does the program pop you back into the roll_dice(6) function with your newly stored variable (user_guess) because my program does nothing after it asks.


#imported to make 'random dice rolls' possible
from random import randint

#imported to simluate the time that it takes to roll dice
from time import sleep

def get_user_guess():
  user_guess = int(raw_input("What is your 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 Max value is: " + str(max_val)
  sleep(1)
  user_guess = get_user_guess()
  if user_guess > max_val:
    print "The number that you have chosen is invalid. Program now exiting..."
    return
  else:
    print "Rolling..."
    sleep(2)
    print "The first value is: %d" % (first_roll)
    sleep(1)
    print str("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 "Congradulations! You've won!"
    else:
      print "You lose."
 
roll_dice(6)


#2

Hi @trpub,

There are problems with this if block ...

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

The condition should be user_guess == total_roll, so that the user wins only by guessing the exact total of the two rolls, rather than also winning if the guess exceeds the total.

For correct spelling,"Congradulations! You've won!", should be changed to "Congratulations! You've won!".

With those two changes, your program should execute correctly whenever the user enters an integer as a response.


#3

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