can you review this python code and tell why it isn't working | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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")

3rd Nov 2018, 12:10 AM
Aws Poseidon
Aws Poseidon - avatar
4 Answers
+ 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 😊
3rd Nov 2018, 2:38 AM
Kishalaya Saha
Kishalaya Saha - avatar
+ 1
You need to erase the breaks.
3rd Nov 2018, 12:17 AM
HonFu
HonFu - avatar
+ 1
ahhhh now i see ... thx Kishalaya Saha 😀😀
3rd Nov 2018, 2:50 AM
Aws Poseidon
Aws Poseidon - avatar
+ 1
You're welcome, Aws Poseidon 😊
3rd Nov 2018, 3:04 AM
Kishalaya Saha
Kishalaya Saha - avatar