0

could someone tell me if anything is wrong i keep getting "You've guessed it wrong" as my output and how can i add number of guesses i can take

a=random.range(1,10)
b=input("enter a number: ") 
if a==b:print("the number you've guessed is correct") 
else:print("You've guessed it wrong")
  • 1
    You’re comparing a number with a string - try `if str(a)==b:` – DisappointedByUnaccountableMod Jun 25 '20 at 18:56
  • You're comparing a number, `a`, with a string, `b`. Those two types never compare equal, even if one is a representation of the other. You probably want `str(a) == b`, `a == int(b)` or something similar (e.g. capturing the input with `b=int(input(...))`). – Blckknght Jun 25 '20 at 18:56

3 Answers3

0

The issue here is that the build in function input() always returns a string. Then later on you are comparing a string with an integer because random.range() always returns a string. One solution would be to turn your input into an integer as I have shown below.

a=random.range(1,10)
b=int(input("enter a number: "))
 
if a==b:
    print("the number you've guessed is correct") 
else:
    print("You've guessed it wrong")

EDIT However I do suggest you put some sort of validation on that input so it does not return an error if the string cannot be changed into a int.

Edwin Cruz
  • 507
  • 3
  • 10
0

you are comaparing string with integer try below code instead of yours it will fine

a=random.range(1,10)
b=int(input("enter a number: "))
if a==b:
    print("the number you've guessed is correct") 
else:
    print("You've guessed it wrong")
Pavan kumar
  • 478
  • 3
  • 16
0

b=input("enter a number: ")

For you to take a number use int(), because input() accepts as string values.

Use:

b = int(input("enter a number: "))

And I thing you should use random.randrange() instead of random.range()

Shivam Jha
  • 3,160
  • 3
  • 22
  • 36