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

时间:2015-04-22 17:12:53

标签: python variables if-statement

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

  

追踪(最近一次呼叫最后一次):

     

文件“D:/。实际   W4 / Lab3Problem1.py“,第54行,in       main()的

     

文件“D:/。实际W4 / Lab3Problem1.py”,第8行,在main中       nestediffunction()

     

文件“D:/。实际W4 / Lab3Problem1.py”,第27行,在nestediffunction中       if(booksread == 0):

     

NameError:名称'booksread'未定义

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

5 个答案:

答案 0 :(得分:5)

变量booksread不在其使用的任何功能nestediffunctioneliffunctionsimpleiffunction的范围内。修改这两个函数,以便您可以接受booksread变量作为参数,然后可以从main传递它。

def simpleiffunction(booksread ):
    ....

def nestediffunction(booksread ):
    ....

def eliffunction(booksread ):
    ....

答案 1 :(得分:2)

您必须在主区域中传入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!')

答案 2 :(得分:1)

我对您的代码进行了一些调整:

class Dummy():
    def __init__(self):
        self.main()

    def main(self):
        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()
        self.nestediffunction(booksread)
        #eliffunction()

    def simpleiffunction(self):
        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(self, booksread):
        self.booksread = booksread
        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(self):
        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!')

Dummy()

1)如果你有一个定义的类,你可以使用__init__,那么当你运行这个类时,这个定义总会被执行。
2)定义总是需要'自我'。
3)booksread在main()中定义,因此其他定义不知道它的存在。因此,如果你把它放在nestediffunction()中,那么它也知道它是什么样的,它有什么价值。

编辑:
如果你不介意的话,我会把你重写。
我让它变得更简单一些,这将向你展示一些更清晰的影响。

class Dummy():
    def __init__(self):
        self.askUser()

    def askUser(self):
        nBooks = int(input('Number of books read: '))
        self.numberPoints(nBooks)

    def numberPoints(self, nBooks):
        self.nBooks = nBooks
        if (nBooks == 0):
            print('You have 0 points!')
        elif (nBooks == 1):
            print('You have 5 points!')
        elif (nBooks == 2):
            print('You have 15 points!')
        elif (nBooks == 3):
            print('You have 30 points!')
        else:
            print('You have 60 points!')

Dummy()

答案 3 :(得分:1)

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

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!')

答案 4 :(得分:0)

你可以按照上面的答案,这些答案绝对正确你可以稍微改变你的代码结构:

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!')

if __name__=="__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()