Tom Dickson Tom Dickson - 5 months ago 7
Python Question

how do i write it to tell whether a number is equal to or not

The PC uses randint to generate a random number between 1 and 10. When i enter a number i want it to be able to tell if it has the same number as me, so i have written this which was working but when i re-ran it, the program stopped working when the numbers were the same and said that i had still won. (you win if you don't have the same number as the PC)

CODE:

print('Your number is: ', player)
answer = input('Confirm? (y/n)')
if answer == ('y') :
print('You have selected ', answer)
print('Game begining in: ')
print('3')
time.sleep(1)
print('2')
time.sleep(1)
print('1')
time.sleep(1)
pc = (randint(1,10))
print('Your number is: ', player, 'and the PC number is:', pc)
if pc == player :
print('You lose! Suck din')
elif pc != player :
print('You Win!')
elif answer == ("n") :
loop =

Answer

the issue is that when you read from command line the variable will be of type str

while the pc variable you generated is of type int

so you are comparing int to str which will always give you that they are not equal to solve this do the following in compare line

if str(pc) == player :
Comments