-4

My code keeps on saying that it has invalid character identifier.

import random
answer1 = ("Absolutely!")
answer2 = ("No way Pedro!")
answer3 = ("Go for it tiger.")
input("Welcome to the Magic 8 Ball game and use it to answer your questions...")
question = int(input("Ask me for any advice and I’ll help you out.  Type in your question and then press Enter for an answer."))
print("shaking.... \n" * 4)
choice = random.randint(1,3)

if choice == 1:
  answer = Answer1
elif choice == 2:
  answer = Answer2
else:
  answer = Answer3
print(answer)
codejockie
  • 9,020
  • 4
  • 40
  • 46

1 Answers1

0

An invalid character in your identifier means that you have something that shouldn't be there. If your error message looks like this:

  File "invalchar.py", line 23
    values =  list(analysis.values ())
            ^
SyntaxError: invalid character in identifier

Then it tells you exactly where the "invalid character" is. I suggest looking over your whole code for any syntax mistakes. Also, I would start using good formatting, like this:

import random

answer1=("Absolutely!")
answer2=("No way Pedro!")
answer3=("Go for it tiger.")

input("Welcome to the Magic 8 Ball game and use it to answer your 
questions...")

question = int(input("Ask me for any advice and I’ll help you out.  
Type in your question and then press Enter for an answer."))

print("shaking.... \n" * 4)
choice=random.randint(1,3)

if choice == 1:
    answer = Answer1

elif choice == 2:
    answer = Answer2

else:
    answer = Answer3
    print(answer)

This makes it easier to read. Also, an invalid character could be a space, so make sure your spacing is correct.

  • 1
    _Also, an invalid character could be a space, so make sure your tabs are correct_ If this were the case, he would be getting `IndentationError`. – John Gordon Dec 16 '17 at 22:59
  • @JohnGordon I changed it to spacing. I do have a question though. If you had 5 spaces instead of four, wouldn't it read as a valid tab and one invalid character? Yeilding an invalid char error instead of an indent error/ – Massimo Daul Dec 16 '17 at 23:02
  • No, that would still be an IndentationError. Invalid Character in Identifier means that the variable name literally contains some character that is not allowed. Usually this is a result of cut-and-pasting code from a PDF or some other heavily formatted document. – John Gordon Dec 16 '17 at 23:07
  • 1
    Kinda suspect that you stole that initial code example from [here](https://stackoverflow.com/a/14844830/1672429). – Stefan Pochmann Dec 16 '17 at 23:36