I have a program to quiz me on how far planets are from the sun. The only problem is, no matter what answer I put it always shows up as correct. Here is a link to my code: http://pastebin.com/MimECyjm
If possible, I would like a more simplistic answer because I am not that proficient in python yet
Code in question:
mercury = "57.9"
mercury2 = "57900000"
def Mercury():
ans = raw_input("How far is Mercury from the sun? ")
if mercury or mercury2 in ans:
print "Correct!"
time.sleep(.5)
os.system("cls")
main()
else:
print "Incorrect!"
Mercury()
The problem is that you have:
This if statement will be
Trueifmercuryevaluates toTrue(which it always does) or whenmercury2 in ansisTrue.mercuryis a non-empty string (mercury = "57.9") which will evaluate toTrue. For example, trybool("57.9")to see that Python always computesTruefor non-empty strings. If the string is empty then it will beFalse.So no matter what the user answers, your code will always say that it is correct. Here’s what you could write:
but it’s probably better to write (see discussion in comments below):