Python变量超出函数语法错误?

2024-09-30 00:30:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我对编程比较陌生。你知道吗

我一直在尝试编写和运行我的第一个程序,我陷入了一个错误。我尝试过几次删除第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

Tags: toyouaddinputrawhavepointsint
1条回答
网友
1楼 · 发布于 2024-09-30 00:30:13

我可以在这里发现两个错误。 第一个在20号线。你已经打开了3个圆括号,只关闭了其中一个圆括号。将第22行改为:

numb = int(raw_input("How many points would you like to add?\n > "))

不需要第三个括号,只需使用2。

第二条是32号线和14号线。您没有将Premains传递到函数dex_skill,因此它无法访问该变量,因此它将返回TypeError。 要解决此问题,请将第32行更改为:

dex_skill(Premains)

第14行:

def dex_skill(Premains):

因此,您的代码如下所示:

Premains = 10 #Remaining points assigned before functions

#Function designed to see user's overall stats and remaining points

def print_skills():
        print "Dexterity:" + dex
        print "Strength:" + str
        print "IntelligenceL:" + int
        print "\nYou have %d remaining" % Premain

#Function called when user selects to edit Dexterity attribute
Premains = 10 #Remaining points assigned before functions

#Function designed to see user's overall stats and remaining points
def print_skills():
        print "Dexterity:" + dex
        print "Strength:" + str
        print "IntelligenceL:" + int
        print "\nYou have %d remaining" % Premain

#Function called when user selects to edit Dexterity attribute

def dex_skill(Premains): #Passing Premains as a parameter so this function can access it.
        Dpoints = 0
        print "Great you choose Dexterity!"
        answer = raw_input("would you like to add or subtract    points?\n > ")

        if answer == "add":
                print "You have: %d points remaining!" % Premains

                numb = int(raw_input("How many points would you like to add?\n > "))

                Premains = (numb - Premains)
                Dpoints = numb + Dpoints

                print "Great! you now have: %d Dexterity points!"

        else:
                print "You choose subtract."
                #Subtract code goes here. Similiar to above.

dex_skill(Premains)

如何纠正语法错误

语法错误是最容易修复的错误之一,您可以在Python抛出的错误中获得修复它所需的所有信息。你知道吗

以下代码:

array = ['john', 'paul', 'mike'

for a in array:
    print(a)

将在第1行返回语法错误,并且Python IDE将突出显示is无法解析过去的点,因此它将如下所示: Example of Syntax Error in Python 2.7

如您所见,for以红色突出显示,因此Python无法解析过去的内容,因此错误要么在那一行,要么在上面的那一行。在本例中,数组已打开,但未关闭,因此Python正在解析for循环,就好像它是一个数组一样,但它不能这样做,因此需要通过更改它来更正第1行。。。你知道吗

由此:array = ['john', 'paul', 'mike'

到这个array = ['john', 'paul', 'mike']

其他IDE应该给您这样的语法错误,或者像官方的PythonIDE为ValueError所做的那样在控制台上打印它

希望这有帮助,迪布斯:)

相关问题 更多 >

    热门问题