标识符4中的字符无效

时间:2017-12-16 22:31:01

标签: python python-3.x

我的代码继续说它有无效的字符标识符。

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)

1 个答案:

答案 0 :(得分:0)

标识符中的无效字符表示您有一些不应该存在的内容。如果您的错误消息如下所示:

  File "invalchar.py", line 23
    values =  list(analysis.values ())
            ^
SyntaxError: invalid character in 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)

这使得阅读更容易。此外,无效字符可能是空格,因此请确保您的间距正确。

相关问题