1
import time
import random
target = random.randrange(1, 3, 2)
print target  #for debug reasons
time.sleep(5)
shot = raw_input("Enter a random number")
while shot != target:
   print "n0pe"
   shot = raw_input("Enter a random number")
else:
    print "you won"

as i was testing this funny game it just always said n0pe even if i was saying the right number. I cant figure out whats wrong with it!

2 Answers 2

2

target is an integer value. raw_input() returns a string. In python, strings and integers will never be equal to each other.

For example:

print 1 == "1"
# prints "False"

You'll want to convert the user input to an integer using int() before doing the comparison.

import time
import random
target = random.randrange(1, 3, 2)
print target  #for debug reasons
time.sleep(5)
shot = int(raw_input("Enter a random number"))
while shot != target:
   print "n0pe"
   shot = int(raw_input("Enter a random number"))
else:
    print "you won"
Sign up to request clarification or add additional context in comments.

1 Comment

THANKS I dont know why i didnt think of that ;D
0

The comparison is not right. You are comparing a string with integer.

Try this:

import time
import random
target = random.randrange(1, 3, 2)
time.sleep(5)
shot = raw_input("Enter a random number")
while True:
    if int(shot) != target:
        print "n0pe"
        shot = raw_input("Enter a random number")
    else:
        print "you won"
        break

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.