我对编程比较陌生。你知道吗
我一直在尝试编写和运行我的第一个程序,我陷入了一个错误。我尝试过几次删除第22行的int(raw\u input()),并做了一些其他的调整,但是我的技能并不能完全解决这个问题。你知道吗
1 Premains = 10 #Remaining points assigned before functions
2
3 #Function designed to see user's overall stats and remaining points
4
5 def print_skills():
6
7 print "Dexterity:" + dex
8 print "Strength:" + str
9 print "IntelligenceL:" + int
10 print "\nYou have %d remaining" % Premain
11
12 #Function called when user selects to edit Dexterity attribute
13
14 def dex_skill():
15 Dpoints = 0
16 print "Great you choose Dexterity!"
17 answer = raw_input("would you like to add or subtract points?\n > ")
18
19 if answer == "add":
20 print "You have: %d points remaining!" % Premains
21
22 numb =(int(raw_input("How many points would you like to add?\n > ")
23
24 Premains = (numb - Premains)
25 Dpoints = numb + Dpoints
26
27 print "Great! you now have: %d Dexterity points!"
28
29 else:
30 print "You choose subtract."
31 #Subtract code goes here. Similiar to above.
32 dex_skill()
~
返回错误
File "try.py", line 24
Premains = (numb - Premains)
^
SyntaxError: invalid syntax
我可以在这里发现两个错误。 第一个在20号线。你已经打开了3个圆括号,只关闭了其中一个圆括号。将第22行改为:
不需要第三个括号,只需使用2。
第二条是32号线和14号线。您没有将
Premains
传递到函数dex_skill
,因此它无法访问该变量,因此它将返回TypeError。 要解决此问题,请将第32行更改为:第14行:
因此,您的代码如下所示:
如何纠正语法错误
语法错误是最容易修复的错误之一,您可以在Python抛出的错误中获得修复它所需的所有信息。你知道吗
以下代码:
将在第1行返回语法错误,并且Python IDE将突出显示is无法解析过去的点,因此它将如下所示:
如您所见,
for
以红色突出显示,因此Python无法解析过去的内容,因此错误要么在那一行,要么在上面的那一行。在本例中,数组已打开,但未关闭,因此Python正在解析for循环,就好像它是一个数组一样,但它不能这样做,因此需要通过更改它来更正第1行。。。你知道吗由此:
array = ['john', 'paul', 'mike'
到这个
array = ['john', 'paul', 'mike']
其他IDE应该给您这样的语法错误,或者像官方的PythonIDE为
ValueError
所做的那样在控制台上打印它希望这有帮助,迪布斯:)
相关问题 更多 >
编程相关推荐