0

I am in the process of making a simple guessing game and have run into a few problems. Firstly I am new to Python and this is my first programming language besides some batch. When I try to execute the following code, the elif and the else do not get interacted with, even when the condition is true. It seems to always respond to the if like it is always true.

#Calder Hutchins 3-1-14
#Guess Game
import time
playGame = ""
guessTry = 3
print "Would you like to play a game? (y/n)"
while playGame != "y":
    playGame = raw_input(">")
    if playGame == "y" or "n":
        print "Great!  Let's get started!"
        print "Answer the following questions correctly to level up!"
    elif playGame == "n":
        print "Very well...."
        time.sleep(2)
        quit()
    else:
        print "Invalid answer, please re-type."
print "end of the while, test."

Also I was wondering about flow control since it is new to me. Is there a way to make it more efficient, the code I pasted.

EDIT: this is not a duplicate... if some of you would of taken the time to actually look at my question you would see it is not. I find it very disappointing to see this happen so many times on Stack Overflow.

Calder Hutchins
  • 755
  • 1
  • 6
  • 10
  • Besides this I think you are not quite understanding the use of if/elseif. I reccomend looking at http://docs.python.org/2/tutorial/controlflow.html – Hugoagogo Mar 02 '14 at 01:25
  • Ok i shall look at it, i am making this script to learn the concept more. – Calder Hutchins Mar 02 '14 at 01:27
  • 1
    With this example I think what you were after for the if statement was simply playGame == "y" if this evaluates to false the program moves onto the next elif statment, if that evaluates to false it will move on to your else case, so you got pretty close. – Hugoagogo Mar 02 '14 at 01:33
  • Yes i wanted it to be like that. But i wanted to have it so if you answered with a in-correct character you would have to type in another answer, a while seemed appropriate. – Calder Hutchins Mar 02 '14 at 01:37
  • 1
    Keep the while loop, try http://pastebin.com/Ey3QVjNZ – Hugoagogo Mar 02 '14 at 01:40
  • Thank-you that worked, but being the curious person i am, how would i have the program not loop if i entered "n"? – Calder Hutchins Mar 02 '14 at 01:47
  • 1
    Use "break" which will exit out of one level of looping http://docs.python.org/2/tutorial/controlflow.html#break-and-continue-statements-and-else-clauses-on-loops – Hugoagogo Mar 02 '14 at 01:49
  • Ok, thankx I think that solves it, wish i could do more to thank you than a few up buttons. – Calder Hutchins Mar 02 '14 at 01:50
  • I think the reason the question got marked as a duplicate is because it suffers a fatal flaw that is explained in the duplicatee. If that isn't actually material to your question, you should fix the bug and *then* ask for it to be reopened. – Kerrek SB Mar 02 '14 at 01:50
  • 2
    I saw this post on the reopen queue and I think rather than accusing others of wrongly duplicate-closing your posts, you should rather describe **in detail** what is the difference between you and the other post. I see no reason to re-open it just because you affirm that it is not a duplicate. – Uli Köhler Mar 02 '14 at 01:51
  • 1
    After taking time to look at your question, it does seem to be a duplicate. The heart of the problem is your `if playGame == "y" or "n":` is not the right way to write that. – Kevin Panko Mar 02 '14 at 01:51
  • 1
    No problem, I had tried to type most of this up in an answer but by the time I had finished it had been marked as a duplicate. Glad to have been able to help. – Hugoagogo Mar 02 '14 at 01:52
  • I can see why you guys would think it a duplicate, but for someone with as little knowledge as i about programming it can be rather hard to see a similarity in another script that neither uses the same structure nor the modules, data types, that i use. – Calder Hutchins Mar 02 '14 at 01:56

1 Answers1

0

Computers are dumb, you need to explicitly state what you want to evaluate.

if playGame == "y" or playgame == "n":

In your version it will always evaluate to true because when the two strings are compared it will get evaluated to True. str or str will always be True

>>> bool('a' or 'c')
True
Vader
  • 6,335
  • 8
  • 31
  • 43