Sign in to follow this  
jordi_0071

[python] what's wrong with this code

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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




Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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 = False
gaveup = False
while 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 = True

if gaveup:
print "You gave up. Loser."
else:
print "You get to continue to the next level."



Share this post


Link to post
Share on other sites
Then your indentation is messed up. you need to make sure that your indentation is consistent.


if a != "Enter":
print "That's not the right word!"
print "Game over"
else:
print "That's the right word"

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this