Help
cancel
Showing results for 
Search instead for 
Did you mean: 
Copilot Lvl 2
Message 1 of 5

Python simple dice

Solved! Go to Solution.

Why this simple python code always prints "incorrect" ?

import random

random_number = random.randint(1, 6)
guess = 0

while True:
guess = raw_input("Enter random number between 1-6: ")
if guess == random_number:
print("correct")

else:
print("incorrect")

4 Replies
Highlighted
Solution
Commander Lvl 2
Message 2 of 5

Re: Python simple dice

Read up on raw_input(). It returns a string.

 

Please follow-up to let us know how you made out. For good karma, mark a reply as the answer if it helped!

Highlighted
Copilot Lvl 2
Message 3 of 5

Re: Python simple dice

Is this how to fix it?

int(input())

Highlighted
Copilot Lvl 2
Message 4 of 5

Re: Python simple dice

The code:

import random

random_number = random.randint(1, 6)

while True:
guess = int(raw_input("Enter random number between 1-6: "))
if guess == random_number:
print("correct")

else:
print("incorrect")

The output:

Output.PNG

Highlighted
Copilot Lvl 2
Message 5 of 5

Re: Python simple dice

Found solution, thanks for help

 

import random


while True:
random_number = random.randint(1, 6)
guess = int(raw_input("Enter random number between 1-6: "))
if guess == random_number:
print("correct")

else:
print("incorrect")