[python] what's wrong with this code

Started by
10 comments, last by smr 18 years, 10 months ago
hey i made this code:

YN = raw_input("Ready to begin(Y/N): ")
if YN == "Y" or "N":

print "Level 1\n\n"

else

print YN = raw_input( "Ready to begin(Y/N): ")



but i cant figure out whats wrong with the code
Advertisement
You forgot the ':' after else and you don't have any indentation in your code. Also you have an excess 'print' there.
Ad: Ancamnia
i did else: but still nothing what else do i have to do?
It's this line:
if YN == "Y" or "N":

In english, this statement makes sense. But to the python interpretter, the statement has a different meaning. In english it couldn't be more simple: if YN is equal to "Y" or "N" then do the following... Python doesn't see it this way. It will not first compare YN with "Y" then compare YN with "N". It will first try to do an OR operation on the two literal values "Y" and "N". This results in the value 'Y'. Then compare the result ('Y') with the value in YN to see if they are like. Thus your code will only work if the user entered "Y".

To fix this, you need replace your if statement with one of the following ifs:

if YN == "Y" or YN == "N":
if YN in ("Y", "N"):

You also need to place the colon (:) after your else and remove the "print" from "print yn = ..." like the other guys said.
edit: i get it but why does the first print has to be deleted?
Only the second print needs to be removed. print can only print the output of expressions that have a result. YN = raw_input(...) doesn't yield a result, thus cannot be printed. In C this could work because an assignments, in addition to actually assigning the value to the variable, also yield the value as a result. Python doesn't work like this. Assignments don't yield results.

Just break it into two lines. It's more readable that way anyway:

YN = raw_input("Ready to begin(Y/N): ")if YN in ("Y", "N", "n", "y"):    print "Level 1\n\n"else:    YN = raw_input( "Ready to begin(Y/N): ")    print YN
ive mad a while loop from it:

while YN == "Y":

"Level 1\n\n"
print "etnrE"
a = raw_input("Typ the right word: ")

if a == "Enter":
print "That's the right word"

if i do it like this

while YN != "Y":

it works but i cant see why cause i though that if its != it suppose to be if its not Y
It's because YN needs to be Y before you ever start. What's happening is the while checks before it ever even starts if YN == "Y". I'm assuming that you didn't put anything into YN before you try to go into the loop.

done = Falsegaveup = Falsewhile not done:    print "Level 1\n\n"    print "etnrE"    a = raw_input("Type the right word: ")    if a == "Enter":        print "That's the right word"        done = True    else:        YN = raw_input("That is incorrect. Try again? (Y/N) ")        if YN in ("N", "n"):            done = True            gaveup = Trueif gaveup:    print "You gave up. Loser."else:    print "You get to continue to the next level."


if i typ Enter it stops but it should print That's the right word:
if a != "Enter": print "That's not the right word!" print "Game over"    else: "That's the right word"

what im doing wrong?
try
print "That's the right word!"

This topic is closed to new replies.

Advertisement