<PLEASE USE THE FOLLOWING TEMPLATE TO HELP YOU CREATE A GREAT POST!>
I have done what the quiz said and it work just fine so I modify it a little. I thought, why not? Here is what I thought.
I know the function int( raw_input() ) allow me to use the number from the user to use in my code. In the original version, I had to call the function roll_dice() in order to run the program and I had to input the number of sides of the dice in the calling code and saved it before I run the program. I thought it would be cool if the user can also decide how many sides of the dice we are going to use in this program so I tried to modify the code so it would receive the input number of sides from the user to run the program before the dice start to roll and before the user can input their guess.
After the modification, the system gave me error messages. I have tried to fix my code many times. Here is the last result of my latest fix. I don’t have any other record of any of my previous attempts trying to fix the code. A few times, the terminal returned "Pick the number of sides: " and after I entered an integer number, the same message appeared over and over again. Sometimes, it successfully run as I want it to, but that happened only once or twice because I changed the code in the middle of before I enter the next number after the "Pick the number of sides: " appear. That times, it broke the loop and work, but once the program end, it ended. Even if I did not change the code, once I started to run “python NumberGuess.py” again, it game me another error messages.
<In what way does your code behave incorrectly? Include ALL error messages.>
<What do you expect to happen instead?>
“Let the game begin!”
Pick the number of sides: user input number of sides
The maximum possible value is: %d
Guess a number: user input number
The first roll is:
The second roll is:
Awesome! You won!
""" This program would roll a pair of dice, then add the values of the roll. Next, it would ask the user to guess that number. After that it will compare the user's guess to the total value and determine the winner. """ from random import randint from time import sleep #get user's raw input def get_user_guess(): guess = int(raw_input("Guess a number: ")) return guess #get number of sides of the dices from user def get_number_of_sides(): number_of_sides = int(raw_input("Pick the number of sides: ")) return number_of_sides #create dices def roll_dice(number_of_sides): number_of_sides = get_number_of_sides() return 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: %d" % max_val guess = get_user_guess() # if guess > max_val: print "Invalid Number! No guessing higher than the maximum possible value!" 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 "Result..." sleep(1) if guess == total_roll: print "Awesome! You won!" else: print "Haha! You lose!" # def guess_number_game(): print "Let the game begin!" get_number_of_sides() roll_dice(number_of_sides) # guess_number_game()