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 theif
andelif
statements. - You can't use
break
inside anif
block. This creates aSyntaxError
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 aValueError
. - Your code looks quite messy. Please follow the standard formatting conventions. There's a tool to automatically fix this, called
autopep8
.