Skip to main content
 首页 » 编程设计

python之我的错误在哪里 (54 行代码)

2025年05月04日13txw1958

这是我当前的代码,当我运行它时,我遇到了一些错误......我不确定它们是什么。

Traceback (most recent call last):

File "D:/.Actual W4/Lab3Problem1.py", line 54, in main()

File "D:/.Actual W4/Lab3Problem1.py", line 8, in main nestediffunction()

File "D:/.Actual W4/Lab3Problem1.py", line 27, in nestediffunction if (booksread == 0):

NameError: name 'booksread' is not defined

def main(): 
    print("Welcome to Ryan's book club!") 
    booksread = int(input('Please enter the amount of books you have read so far: ')) 
#   if (booksread < 0): 
#       print("That's impossible! Please  re-enter the amount of books you have read: ") 
#       booksread1 = int(input('Please enter the amount of books you have read so far: '))         
    #simpleiffunction() 
    nestediffunction() 
    #eliffunction() 
 
 
 
 
def simpleiffunction(): 
    if (booksread == 0): 
        print('You have 0 points!') 
    if (booksread == 1): 
        print('You have 5 points!') 
    if (booksread == 2): 
        print('You have 15 points!') 
    if (booksread == 3): 
        print('You have 30 points!') 
    if (booksread >= 4): 
        print('You have 60 points!') 
 
def nestediffunction(): 
    if (booksread == 0): 
        print('You have 0 points!') 
    else: 
        if (booksread == 1): 
            print('You have 5 points!') 
        else: 
            if (booksread == 2): 
                print('You have 15 points!') 
            else: 
                if (booksread == 3): 
                    print('You have 30 points!') 
                else: 
                    if (booksread >= 4): 
                        print('You have 60 points!') 
 
def eliffunction(): 
    if (booksread == 0): 
        print('You have 0 points!') 
    elif (booksread == 1): 
        print('You have 5 points!') 
    elif (booksread == 2): 
        print('You have 15 points!') 
    elif (booksread == 3): 
        print('You have 30 points!') 
    elif (booksread >= 4): 
        print('You have 60 points!') 
 
main() 

请您参考如下方法:

变量 booksread 不在它使用的任何函数的范围内 nestediffunction、eliffunction 和 simpleiffunction .修改这两个函数,以便您可以接受 booksread 变量作为参数,并且可以相应地从 main 传递它。

def simpleiffunction(booksread ): 
    .... 
 
def nestediffunction(booksread ): 
    .... 
 
def eliffunction(booksread ): 
    ....