Jump to content
  • Advertisement
Sign in to follow this  

[python] what's wrong with this code

This topic is 4736 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
You forgot the ':' after else and you don't have any indentation in your code. Also you have an excess 'print' there.

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
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?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!