-2

Been round the houses on this for a few hours trying to figure out how to make this code execute, but its just making no sense to me anymore.

For some reason I receive the Invalid character in identifier error for the variable answer. I believe it has something to do with the fact that you can assign a value to a variable inside a conditional statement but you can't declare a new variable inside a conditional statement.

However despite trying to declare answer outside of the IF statement, declaring inside and generally trying my luck at altering bits and bobs here and there - I just cannot seem to work this out.

a = 2
b = 3
c = 10
if a == 2 * (b * c):
    answer = 'a is double the sum of the others'
if b == 2 * (a + c):
    answer = 'b is double the sum of the others'
if c == 2 * (a * b):
    answer = 'c is double the sum of the others'
if a != 2 * (b + c) and b != 2 * (a + c) and c != 2 * (a + b):
    answer = 'No number is double the sum of the others'
print(answer)

Picture of the error i'm receiving, it was also throwing this error for the first instance of 'answer' in the first IF statement. Image

  • What happens if you write `answer = ""` in a line after `c = 10`? – Dani Mesejo Oct 23 '21 at 22:31
  • 1
    Of course you can assign to a new variable inside an `if` block (python doesn't have variable *declarations*). If I run your code, it `NameError`s because `answer` is not defined if none of the `if` blocks execute. – Antimon Oct 23 '21 at 22:33
  • 1
    Could it be that your if statements are defined incorrectly? (eg. instead of `a == 2 * (b * c)`, it should be `a == 2 * (b + c)`) – Alessandro Oct 23 '21 at 22:34
  • @DaniMesejo - It returns the same 'Invalid character in identifier error' however it highlights the second instance of 'answer' from the 'if b==2' IF statement. – user17230533 Oct 23 '21 at 22:36
  • @Antimon - Interesting, for some reason the Python IDLE is returning the invalid character error, I havent yet received a NameError. – user17230533 Oct 23 '21 at 22:38
  • 1
    @user17230533 Could you please edit the question to include the full traceback then? – Michael Wheeler Oct 23 '21 at 22:41
  • 1
    @user17230533 Please try to run your script from a command prompt and let us know what happens. – Antimon Oct 23 '21 at 22:42
  • See https://stackoverflow.com/questions/14844687/invalid-character-in-identifier. – Nikolaos Chatzis Oct 23 '21 at 22:46
  • Some vague suspicion here... did you copy-paste the code from somewhere else? If so, maybe some non-printing characters have been carried along. Try re-typing the code by yourself and see what happens. Just from the looks of it, this code shouldn't raise the error you're seeing. – Antimon Oct 23 '21 at 22:47
  • 1
    Echoing what @NikolaosChatzis and others have said, when I use the code included in your question text, and follow the debug instructions of the [top answer](https://stackoverflow.com/a/14844830/8857601) posted at the linked question, I can't detect any unusual characters. What do you get when you copy and paste exactly from your editor into the IDLE as described in the above link? – Michael Wheeler Oct 23 '21 at 22:50
  • Antimon, Michael - I have resolved this issue however i'm not sure how to mark the issue as resolved on this website. Your last suspicion was correct, it was code that I copy and pasted and that was the issue, when I received helpful alternative code blocks on this post I created a new file and realised I no longer received the error. I'm not sure why this should be but I have a feeling that the source text encoding standard and the standard used on this website are different so therefore larger/smaller memory words were expected guiding the compiler to throw this error. Thanks for your help – user17230533 Oct 24 '21 at 10:35
  • @user17230533 You can accept an answer, as shown here: https://meta.stackexchange.com/a/5235 – Michael Wheeler Oct 25 '21 at 16:40

4 Answers4

6

The thing that stands out to me, looking at the screenshot you provided, is that things aren't quite lining up - literally (blue vertical lines added by me):

This looks like a monospace font to me (every character has the same width). The answer identifier in the first indented block does not line up with all the others, however.

Confirm that your four indented answer variables are each indented by FOUR spaces or a single tab character, and not some weird, non-printable control characters.

Paul M.
  • 10,481
  • 2
  • 9
  • 15
0

As you very well described, the problem is caused by the fact that you do not have the answer variable declared before the if structure.

For the current values of a, b, c - none of the if conditions are actually true, so the variable doesn't get initialised.

Try assigning it a default value before the if statements.

Also, you could look into converting those multiple IFs in an IF-ELIF structure.

Maybe your code should like this:

a = 2
b = 3
c = 10
answer = "Default value"
if a == 2 * (b * c):
    answer = 'a is double the sum of the others'
elif b == 2 * (a + c):
    answer = 'b is double the sum of the others'
elif c == 2 * (a * b):
    answer = 'c is double the sum of the others'
elif a != 2 * (b + c) and b != 2 * (a + c) and c != 2 * (a + b):
    answer = 'No number is double the sum of the others'
print(answer)

Other way to go is to replace the last condition with an else statement, and not assign an initial value to the answer. This would mean eliminating the last condition.

a = 2
b = 3
c = 10
if a == 2 * (b * c):
    answer = 'a is double the sum of the others'
elif b == 2 * (a + c):
    answer = 'b is double the sum of the others'
elif c == 2 * (a * b):
    answer = 'c is double the sum of the others'
else:
    answer = 'No number is double the sum of the others'
print(answer)
pyhalex
  • 19
  • 2
0

As @Antimon said, you're seeing an error because answer doesn't get assigned precisely because each of the above conditions is false. Check this with an else clause:

a = 2
b = 3
c = 10
if a == 2 * (b * c):
    answer = 'a is double the sum of the others'
if b == 2 * (a + c):
    answer = 'b is double the sum of the others'
if c == 2 * (a * b):
    answer = 'c is double the sum of the others'
if a != 2 * (b + c) and b != 2 * (a + c) and c != 2 * (a + b):
    answer = 'No number is double the sum of the others'
else:
    answer = 'None of the above conditions were met!'
print(answer)

The reason that you're not catching it with the bottom one a != 2 * (b + c) and b != 2 * (a + c) and c != 2 * (a + b) is because you seem to have typo'ed * in place of +:

if a == 2 * (b + c):  # Once here
    ...
if c == 2 * (a + b):  # and again here
    ...
Michael Wheeler
  • 849
  • 1
  • 10
  • 29
  • 2
    That's actually not quite what I'm saying. `answer` not being defined will raise a `NameError`. In his case, the "invalid identifier" error seems to indicate that some character weirdness has crept into the code. – Antimon Oct 23 '21 at 22:39
0

Check out what is inside your conditional statement. Your phrasing says that you want to check if the number is double the sum of the other two numbers however, you check for the product. Try ...

a = 2
b = 3
c = 10

if a == 2 * (b + c):
    answer = 'a is double the sum of the others'
elif b == 2 * (a + c):
    answer = 'b is double the sum of the others'
elif  c == 2 * (a + b):
    answer = 'c is double the sum of the others'
elif  a != 2 * (b + c) and b != 2 * (a + c) and c != 2 * (a + b):
    answer = 'No number is double the sum of the others'
else:
    answer = 'Error: none of the specified options happened'
print(answer)