1

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()
2
  • 2
    There are 150 lines of code, can you narrow down the problem and just post the code that is relevant here? (That process will also deepen your understanding of the code)
    – Levon
    Aug 14, 2012 at 16:14
  • 1
    SSCCE
    – Robert
    Aug 14, 2012 at 16:15

3 Answers 3

6

The problem is that you have:

if mercury or mercury2 in ans:

This if statement will be True if mercury evaluates to True (which it always does) or when mercury2 in ans is True.

mercury is a non-empty string (mercury = "57.9") which will evaluate to True. For example, try bool("57.9") to see that Python always computes True for non-empty strings. If the string is empty then it will be False.

So no matter what the user answers, your code will always say that it is correct. Here's what you could write:

if mercury in ans or mercury2 in ans:

but it's probably better to write (see discussion in comments below):

if ans in [mercury, mercury2]:
5
  • 4
    Or possibly if ans in [mercury, mercury2]
    – Deestan
    Aug 14, 2012 at 16:18
  • @Deestan: That will work as well but it does mean that the user must answer with only a number. If they answer "57900000 (roughly)" then it would be wrong. Aug 14, 2012 at 16:21
  • 1
    Well yes, but the other alternative has false positives. "34539485934557.99" would be accepted as correct.
    – Deestan
    Aug 14, 2012 at 16:23
  • @Deestan: Good point, that is even worse. I'll update my answer. Aug 14, 2012 at 16:24
  • Thanks, my code is running much better now! Now to get back to memorizing the ESRT... Aug 14, 2012 at 16:28
6

you have this:

if mercury or mercury2 in ans:

instead of this:

if ans in (mercury, mercury2):

However you have a deeper problem. Code like this

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()

will eventually cause a stackoverflow. This is because you are calling functions, but never returning from them!

You should restructure the code to use while loops

You should also consider removing some of the duplication from the program

eg You could use a function like this

def main():
    while True:    
        print "Planetary Distance from the Sun"
        time.sleep(.5)
        rand = random.randint(1,1)
        if rand==1:
            ask_planet_distance("Mercury", mercury, mercury2)
        elif rand==2:
            ask_planet_distance("Venus", venus, venus2)
        ...


def ask_planet_distance(planet_name, distance1, distance2):
    while True:
        ans = raw_input("How far is {} from the sun? ".format(planet_name))
        if ans in (distance1, distance2):
            break
        else:
            print "Incorrect!"
    print "Correct!"
    time.sleep(.5)
    os.system("cls")

You can go even further by storing the planet data in a list

1
  • +1 for the very important observation never returning from Mercury()
    – chepner
    Aug 14, 2012 at 16:29
3

The problem is with your if statement conditions.

Example:

if ans == venus or venus2:

This should be:

if ans == venus or ans == venus2:

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.