0
can you review this python code and tell why it isn't working
#This is "guess the dice game " from random import randint print("""Welcome!, to guess the dice game You will have to guess the numbers that appears on the dice""") welcome_msg = "\nenter a number bettwen one and six" player_input = int(input(welcome_msg)) print("\nyou guessed : " + str(player_input)) if player_input>6: print("\n ivnalid input") break elif player_input<1: print("\n invalid input") break dice = randint(1,6) if player_input == dice: print("\n the dice showed the number :" + str(dice)) print("\nyou win!!") elif player_input != dice: print("\n the dice showed the number :" + str(dice)) print ("\noh oh, you lost")
4 Respuestas
+ 5
To elaborate on HonFu 's answer, the problem occurs because your break statements don't match the indentation of the print statements preceding them. We need to use the same amount of indentation for the whole if block. Moreover, the break statements are for breaking out of loops (for/while). They won't really help us here. If you want to stop the flow of the code, use exit() from the sys module. So it should look like
import sys
# first part of your code here
if player_input > 6:
print("\n ivnalid input")
sys.exit()
elif player_input < 1:
print("\n invalid input")
sys.exit()
# rest of your code here
Please let me know if that doesn't work! Thanks 😊
+ 1
You need to erase the breaks.
+ 1
ahhhh now i see ... thx Kishalaya Saha 😀😀
+ 1
You're welcome, Aws Poseidon 😊