Home > database >  I want to extend my program so that it displays if the user guessed the correct number, guessed too
I want to extend my program so that it displays if the user guessed the correct number, guessed too

Time:02-17

def dice_game():
    print ("Welcome to my dice game")
import random
your_name = input ("Please enter your name:")
your_lucky_number = int(input ("Please select a lucky number between 1 and 6:"))
print (your_name,"'s lucky number was:", your_lucky_number)
#initialize computer number
computer_die_roll = random.randint(1,6)
print ("The computer rolled: ", computer_die_roll)

if your_lucky_number == computer_die_roll:
    print("You guessed correct, well done!")
    break
elif your_lucky_number < computer_die_roll:
    print("You guessed too low!")
    else:
        print("You guessed too high!")

CodePudding user response:

There are several issues with your code, roughly in order of severity:

  • The indentation of the else block is wrong. Dedent it so that it's on the same level as the if and elif statements.
  • You can't use break inside an if block. This creates a SyntaxError so remove it.
  • You don't verify your number input and blindly cast it to int which breaks when you enter anything that isn't an integer by throwing a ValueError.
  • Your code looks quite messy. Please follow the standard formatting conventions. There's a tool to automatically fix this, called autopep8.
  • Related