-2

So I made a random number guesser, but I'm not sure why it's not working. When The number is correct, it should break the loop, and print yes.

import random
import math
print("Im thinking of a number between 1 and 10,what is it?")
num = random.randint(0,10)
while True:
  x = input("")
  if x == num:
    print("Good Job!")
    break
  elif x != num:
    print("Nope")```
sj95126
  • 6,520
  • 2
  • 15
  • 34

1 Answers1

1

You are comparing string with int. Convert user input (string) to int and then compare.

You can convert using int(x) == num.

One more thing, randint(0,10) will also return 0, you need to use random.randint(1,10).

Live example:

<script defer src="https://pyscript.net/unstable/pyscript.js"></script>
<link rel="stylesheet" href="https://pyscript.net/alpha/pyscript.css" />


<div id="out"></div>
<py-script output="out" style="display: none">
import random
import math
print("Im thinking of a number between 1 and 10,what is it?")
num = random.randint(1,10)

while True:
  x = input("")
  if int(x) == num:
    print("Good Job!")
    break
  else:
    print("Nope")
</py-script>
KiraLT
  • 2,385
  • 1
  • 24
  • 36