-2

I'm working on improving my python 3 skills by creating this simple game.

Very basic, you have a random number between 1 and 5 and if you guess it correct them you win. However for reason reason whenever I try to run it all I get is the "you lose" result even when my test print shows I got the same number.

#!/usr/bin/python3
import random

B = input("Pick a number between 1 and 5:" )
F = random.randrange(1, 5, 1)

if B == F:
    print("You win")
else:
    print("You lose")
print (B, F)

I can't tell if it's the == function that is causing the problem or the if function is wrong for some reason but it doesn't look it.

Ami Tavory
  • 74,578
  • 11
  • 141
  • 185
Oliver at ontoit
  • 185
  • 1
  • 2
  • 13

3 Answers3

3
import random

B = int(input("Pick a number between 1 and 5 ")) # Input goes inside int()
F = random.randrange(1,5,1)

if B == F:
    print("you win")
else:
    print("you lose")
print(B, F)

Side Note: The code you produced actually works fine in Python 2 where input returns an int if appropriate.

David Greydanus
  • 2,551
  • 1
  • 23
  • 42
1

Cast the input in order to get an int

#!/usr/bin/python3
import random

B = int(input("Pick a number between 1 and 5 " ))
F =(random.randrange(1,5,1))

if B == F:
    print("you win")
else:
    print("you lose")
print (B, F)
Avión
  • 7,963
  • 11
  • 64
  • 105
0
B = int(input("Pick a number between 1 and 5 " ))

Change the type for the input to an integer.

Ernesto
  • 295
  • 2
  • 13