问题
This question already has an answer here:
- How to test multiple variables against a value? 24 answers
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:
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")
回答2:
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.
回答3:
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
回答4:
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:
- It's "if", not "If". Python is case sensitive.
- Indentation is important.
- There is no colon or semi-colon at the end of python commands.
- You want raw_input not input;
input
evals the input. - "or" gives you the first result if it evaluates to true, and the second result otherwise. So
"a" or "b"
evaluates to"a"
, whereas0 or "b"
evaluates to"b"
. See The Peculiar Nature of and and or.
回答5:
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.
回答6:
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
来源:https://stackoverflow.com/questions/6762959/if-statement-for-strings-in-python