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

2024-09-29 23:24:14 发布

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

这是我当前的代码,当我运行它时,我得到一些错误…我不知道他们是什么。你知道吗

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()

Tags: youifmaindefhaveelsepointsfile
3条回答

必须在主区域中传入booksread变量。这允许函数知道用户输入的书籍的编号。函数使用numbooksread变量存储传递的变量,以便在函数中使用。你知道吗

nestediffunction(booksread)

def nestediffunction(numbooksread):
if (numbooksread == 0):
    print('You have 0 points!')
else:
    if (numbooksread == 1):
        print('You have 5 points!')
    else:
        if (numbooksread == 2):
            print('You have 15 points!')
        else:
            if (booksread == 3):
                print('You have 30 points!')
            else:
                if (numbooksread >= 4):
                    print('You have 60 points!')

默认情况下,函数中的变量不是全局变量,因此其他函数中不存在该变量。您需要将其作为属性传递:

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(booksread)
    #eliffunction()

def nestediffunction(booksread):
    if booksread == 0: # Also, you don't need parenthesis here
        print('You have 0 points!')

等等

变量booksread不在它使用的任何函数nestediffunctioneliffunctionsimpleiffunction的范围内。修改这两个函数,以便可以接受booksread变量作为参数,并相应地从main传递它。你知道吗

def simpleiffunction(booksread ):
    ....

def nestediffunction(booksread ):
    ....

def eliffunction(booksread ):
    ....

相关问题 更多 >

    热门问题