29

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()
user829084
  • 441
  • 1
  • 5
  • 5
  • 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! – samb8s Jul 20 '11 at 13:55

6 Answers6

54

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")
Daniel DiPaolo
  • 55,313
  • 14
  • 116
  • 115
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.

Björn Pollex
  • 75,346
  • 28
  • 201
  • 283
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

JAB
  • 20,783
  • 6
  • 71
  • 80
  • @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. – JAB Jul 20 '11 at 13:58
  • sorry, I added that before you edited your post :) – samb8s Jul 20 '11 at 14:18
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.
Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
MGwynne
  • 3,512
  • 1
  • 23
  • 35
  • 2
    Your fifth point is incorrect. `x or y` will return the first value if it evaluates to `true`, and the second value otherwise. – Björn Pollex Jul 20 '11 at 13:55
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.

agf
  • 171,228
  • 44
  • 289
  • 238
  • "`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. – JAB Jul 20 '11 at 14:00
-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

viraptor
  • 33,322
  • 10
  • 107
  • 191