Re: [HELP] Python Problem
Quote:
Originally Posted by
Andy Pipkin
The code was all fine until I added the "else"
The problem occurs after adding else. The rest of the code is fine.
I'm guessing you missed out a colon after 'else'. Make sure it's else: instead of just else
Quote:
Originally Posted by
Mapplexploits
OP means Original poster.
Use input then. Never said it was bad to use. I just said it was different then the raw_input function.
Dont tell me what to do please.
raw_input works in Python 2.x but input doesn't while for Python 3.x only input is available.
I'm just saying though.
Re: [HELP] Python Problem
It still does not work. http://gyazo.com/2fa2c83becdcfddbe03...png?1350063732
What Im trying to do is, when they input left or right, they go further by when they enter anything else I want a message to appear...
Re: [HELP] Python Problem
Could you post the whole of your current code?
Re: [HELP] Python Problem
Code:
def maze1():
print("You enter the maze...")
print("You reach a opening in the wall and go through it...")
print()
print("You can go left or right")
answer = input ("Make your choice...: ")
if answer == "Left" or "left":
print("...you fall down a trap door and are never seen again....")
elif answer == "right" or "Right":
print("...you see a beautiful grassy path lined with trees with a pot of gold at the end!")
else: print("Let me remind you, that you can only enter 'Right' or 'Left'")
Re: [HELP] Python Problem
Quote:
Originally Posted by
Andy Pipkin
Code:
def maze1():
print("You enter the maze...")
print("You reach a opening in the wall and go through it...")
print()
print("You can go left or right")
answer = input ("Make your choice...: ")
if answer == "Left" or "left":
print("...you fall down a trap door and are never seen again....")
elif answer == "right" or "Right":
print("...you see a beautiful grassy path lined with trees with a pot of gold at the end!")
else: print("Let me remind you, that you can only enter 'Right' or 'Left'")
Code:
print("You enter the maze...")
print("You reach a opening in the wall and go through it...")
print()
print("You can go left or right")
answer = input ("Make your choice...: ")
if answer == "Left":
print("...you fall down a trap door and are never seen again....")
elif answer == "right":
print("...you see a beautiful grassy path lined with trees with a pot of gold at the end!")
else:
print("Let me remind you, that you can only enter 'Right' or 'Left'")
Use this. Make sure the indentations are the same.
In your code, you used if answer == "Left" or "left". By doing so, it will, in order, do the following :
1) Check if answer == "Left". If this it true, it executes the specified block.
2) If answer is not equals to "Left", check if "left". This is the tricky part. For MOST programming languages (including C,C++,C#,Java), specifying a value not equal to zero for an if conditional will ALWAYS return true. Which means, when it checks if "left", the conditional will always return true. This is why in C, you usually see people using "while(1)", it just means "while(true)"
The correct way to check if answer equals either "left" or "Left" is:
if answer == "Left" or answer == "left":
Re: [HELP] Python Problem
Quote:
Originally Posted by
Jash
Code:
print("You enter the maze...")
print("You reach a opening in the wall and go through it...")
print()
print("You can go left or right")
answer = input ("Make your choice...: ")
if answer == "Left":
print("...you fall down a trap door and are never seen again....")
elif answer == "right":
print("...you see a beautiful grassy path lined with trees with a pot of gold at the end!")
else:
print("Let me remind you, that you can only enter 'Right' or 'Left'")
Use this. Make sure the indentations are the same.
In your code, you used if answer == "Left" or "left". By doing so, it will, in order, do the following :
1) Check if answer == "Left". If this it true, it executes the specified block.
2) If answer is not equals to "Left", check if "left". This is the tricky part. For MOST programming languages (including C,C++,C#,Java), specifying a value not equal to zero for an if conditional will ALWAYS return true. Which means, when it checks if "left", the conditional will always return true.
The correct way to check if answer equals either "left" or "Left" is:
if answer == "Left" or answer == "left":
It works but i need it so it works if it says "left or Left" :/
Re: [HELP] Python Problem
Quote:
Originally Posted by
Andy Pipkin
It works but i need it so it works if it says "left or Left" :/
Use the following instead:
if answer == "Left" or answer == "left":
You should not use :
if answer == "Left" or "left"
because the compiler sees it as:
if (answer == "Left") or "left"
which will always evaluate to true because of the "left"
Re: [HELP] Python Problem
Re: [HELP] Python Problem
Quote:
Originally Posted by
Andy Pipkin
Thanks :)
I highly recommend you read this; PEP 8 -- Style Guide for Python Code
Re: [HELP] Python Problem
Quote:
Originally Posted by
Lewis
Hey, My programming teacher gave me a task to do on python.
I managed to get to the very end but I got stuck on the last bit,
here is my code.
Code:
def maze1():
print("You enter the maze...")
print("You reach a opening in the wall and go through it...")
print()
print("You can go left or right")
answer = input ("Make your choice...: ")
if answer == "Left" and "left":
print("...you fall down a trap door and are never seen again....")
elif answer == "right" and "Right":
print("...you see a beautiful grassy path lined with trees with a pot of gold at the end!")
else print("Let me remind you, that you can only enter 'Right' or 'Left'")
I am trying to get it to use the "else" If they input anything other than "right,Right" Or "Left,left"
When every you input something different it shows one of the if or elif answers.
Please help ):
Dude, Python3 is muuuch better than Python 2.x. The only advantage of Py2k is there are more applications, more modules, and more people using it.
You're code 'and "left"' and similarly, 'and "Right"' is unneeded. Actually, the reason the left works and the right does not is because you put the upper-case 'Right' on the opposite side than you did the upper case 'Left'. Using the 'upper' string method will make sure 'left', 'Left', 'lEft', and 'LEFT' are all the same. Sp take Mapplexploits's advice on that point.
Good choice, Py3k is one of my all-time favorite programming languages.