Beginner Python – Little help on simple code

Question:

I am super new to Python and programming and am trying to digest some basics I’ve learned.

I am stuck on a running a question in python and returning differing statements depending on the reply – yes/no.

Here’s my code;

answer = input('Would you go on strike')
if answer == 'yes':
    print ('You are't a strikebreaker')
else:
    print ('You are a strikebreaker')

Whatever I answer, python prints the ‘else’ response. I have checked a few examples and tutorials and that appears to be the simple code.

Please help?

I followed a few tutorials and expected to get different print statements correlating to the input response.

Asked By: St Lewis

||

Answers:

Strange, what you’re doing should work, I just tried it out myself.

Be aware that string comparisson is case sensetive.

What version of python are you running? Make sure you are using python 3.x.

If you use python 2.x you will have to use raw_input() instead of input()

Hope this helps

Answered By: aronb

I’ve checked your code and it works fine. I think that you are making the mistake of writing "yes" on a new line.

You need to type "yes" like this:
console

What you might be looking for is a newline. To fix this, in your code when you ask the input, you could enter a "newline character. The newline character in Python is "n" (backslash). So your code would be:

answer = input('Would you go on strike:n') #Notice the newline character?
if answer == 'yes':
    print ('You are't a strikebreaker')
else:
    print ('You are a strikebreaker')

The result of the new code would be:enter image description here

Answered By: billyBob456
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.