Tom Dickson Tom Dickson - 1 year ago 71
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)


print('Your number is: ', player)
answer = input('Confirm? (y/n)')
if answer == ('y') :
print('You have selected ', answer)
print('Game begining in: ')
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 Source

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 :
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download