31

I am a total beginner and have been looking at http://en.wikibooks.org/wiki/Python_Programming/Conditional_Statements but I can not understand the problem here. It is pretty simple, if the user enters y it should print this will do the calculation although I get a syntax error on IF answer=="y"

answer = str(input("Is the information correct? Enter Y for yes or N for no"))
proceed="y" or "Y" 
If answer==proceed:
print("this will do the calculation"):
else:
exit()
1
  • 9
    the answers below should help you. But it looks like maybe you need to start from scratch and follow a programming tutorial. Best to learn the basics before diving in! Commented Jul 20, 2011 at 13:55

6 Answers 6

56

Even once you fixed the mis-cased if and improper indentation in your code, it wouldn't work as you probably expected. To check a string against a set of strings, use in. Here's how you'd do it (and note that if is all lowercase and that the code within the if block is indented one level).

One approach:

if answer in ['y', 'Y', 'yes', 'Yes', 'YES']:
    print("this will do the calculation")

Another:

if answer.lower() in ['y', 'yes']:
    print("this will do the calculation")
Sign up to request clarification or add additional context in comments.

4 Comments

Note also that in the code as the OP posted it, the indentation is wrong.
You missed the problem that str(input()) won't work, raw_input() is correct.
@agf that depends on which version of Python he's using, though if it would work (ie, using Python 3.x) it would be redundant.
That's good to know -- I missed that change. Thanks.
14

If should be if. Your program should look like this:

answer = raw_input("Is the information correct? Enter Y for yes or N for no")
if answer.upper() == 'Y':
    print("this will do the calculation")
else:
    exit()

Note also that the indentation is important, because it marks a block in Python.

5 Comments

Still wrong. raw_input(), not str(input()).
@agf: You are right, fixed that.
I had to take the colon out after the print("this will do the calculation")
@Mr.Tea That is indeed an error, I've fixed that, thanks!
@BjörnPollex you probably haven’t seen your answer in seven years!!! Hahaha still being used though :)
7

Python is a case-sensitive language. All Python keywords are lowercase. Use if, not If.

Also, don't put a colon after the call to print(). Also, indent the print() and exit() calls, as Python uses indentation rather than brackets to represent code blocks.

And also, proceed = "y" or "Y" won't do what you want. Use proceed = "y" and if answer.lower() == proceed:, or something similar.

There's also the fact that your program will exit as long as the input value is not the single character "y" or "Y", which contradicts the prompting of "N" for the alternate case. Instead of your else clause there, use elif answer.lower() == info_incorrect:, with info_incorrect = "n" somewhere beforehand. Then just reprompt for the response or something if the input value was something else.


I'd recommend going through the tutorial in the Python documentation if you're having this much trouble the way you're learning now. http://docs.python.org/tutorial/index.html

2 Comments

@samb: What about the or? Because "y"'s boolean value is True, the result of proceed = "y" or "Y" will just be the assignment of "y" to proceed, which doesn't take into account the uppercase case. Daniel's usage of the in operator is good, though.
sorry, I added that before you edited your post :)
7

You want:

answer = str(raw_input("Is the information correct? Enter Y for yes or N for no"))
if answer == "y" or answer == "Y":
  print("this will do the calculation")
else:
  exit()

Or

answer = str(raw_input("Is the information correct? Enter Y for yes or N for no"))
if answer in ["y","Y"]:
  print("this will do the calculation")
else:
  exit()

Note:

  1. It's "if", not "If". Python is case sensitive.
  2. Indentation is important.
  3. There is no colon or semi-colon at the end of python commands.
  4. You want raw_input not input; input evals the input.
  5. "or" gives you the first result if it evaluates to true, and the second result otherwise. So "a" or "b" evaluates to "a", whereas 0 or "b" evaluates to "b". See The Peculiar Nature of and and or.

1 Comment

Your fifth point is incorrect. x or y will return the first value if it evaluates to true, and the second value otherwise.
3
proceed = "y", "Y"
if answer in proceed:

Also, you don't want

answer = str(input("Is the information correct? Enter Y for yes or N for no"))

You want

answer = raw_input("Is the information correct? Enter Y for yes or N for no")

input() evaluates whatever is entered as a Python expression, raw_input() returns a string.

Edit: That is only true on Python 2. On Python 3, input is fine, although str() wrapping is still redundant.

1 Comment

"input() evaluates whatever is entered as a Python expression, raw_input() returns a string." Except in Python 3, where input() does what raw_input() used to, and you'd use eval(input()) or something if you wanted the input to be evaluated as a Python expression.
-1

Python is case sensitive and needs proper indentation. You need to use lowercase "if", indent your conditions properly and the code has a bug. proceed will evaluate to y

1 Comment

proceed will not evaluate to true, it will evaluate to 'y'.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.