我该如何解决这个语法错误

时间:2018-11-23 09:18:56

标签: python

现在我正在用python编写王牌游戏,但由于此语法错误(通常不是实际的语法错误)而无法运行。 这是我的代码:

def CPUcategory():
    CPU = ["exercise","intelligence","friendliness","drool"]
    CPUchoice = random.randint(0,3)
    print("CPU chooses",CPU[CPUchoice])
    time.sleep(3)
    category = CPUchoice + 1
    return category

但是它指出语法错误是在"之后的print(上。 Screenshot of code

CPU下的项目列表是给定的类别,我被用作王牌类别。

如果有人能告诉我为什么会出现此错误以及如何解决该错误,我将不胜感激

1 个答案:

答案 0 :(得分:-1)

您使用哪个版本的python和IDE?

因为问题不在于代码。

Python2.7-好的:

$ python
Python 2.7.15rc1 (default, Nov 12 2018, 14:31:15) 
[GCC 7.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import time
>>> import random
>>> def CPUcategory():
...     CPU = ["exercise","intelligence","friendliness","drool"]
...     CPUchoice = random.randint(0,3)
...     print("CPU chooses",CPU[CPUchoice])
...     time.sleep(3)
...     category = CPUchoice + 1
...     return category
... 
>>> CPUcategory()
('CPU chooses', 'intelligence')
2
>>> 

Python3-好的:

python3
Python 3.6.6 (default, Sep 12 2018, 18:26:19) 
[GCC 8.0.1 20180414 (experimental) [trunk revision 259383]] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import time
>>> import random
>>> def CPUcategory():
...     CPU = ["exercise","intelligence","friendliness","drool"]
...     CPUchoice = random.randint(0,3)
...     print("CPU chooses",CPU[CPUchoice])
...     time.sleep(3)
...     category = CPUchoice + 1
...     return category
... 
>>> CPUcategory()
CPU chooses exercise
1
>>>

致谢,
伊霍尔。