1

This is my current code and when I run it, I get a few errors...and I'm not sure what they are.

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

5 Answers 5

5

The variable booksread is not in scope of any of the function it uses nestediffunction, eliffunction and simpleiffunction. Modify both these function such that you can accept booksread variable as a parameter and you can accordingly pass it from main.

def simpleiffunction(booksread ):
    ....

def nestediffunction(booksread ):
    ....

def eliffunction(booksread ):
    ....
Sign up to request clarification or add additional context in comments.

3 Comments

I added it so it looks like def simpleiffunction(booksread ): and so on with the others, but I get the same errors. Traceback (most recent call last): File "D:/.Actual W4/Lab3Problem1.py", line 54, in <module> main() File "D./.Actual W4/Lab3Problem1.py", line 8, in main nestediffunction() TypeError: nestediffunction() missing 1 required positional argument: 'booksread'
@Ryan: You have to change the signature of all the functions. Moreover, your function call must match the signature i.e. you need to pass booksread as the parameter to all the changed functions
Could you dumb this down? I'm having trouble understanding.
2

You have to pass in the booksread variable in your main area. This allows the function to know the number the books the user entered. The function uses numbooksread variable to stored the passed variable so it can be used in the function.

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

Comments

1

A variable in a function is not global at default, so it doesen't exist for the other functions. You need to pass it as an attribute:

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

Etc.

Comments

1

I made some adjustments to your code:

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) If you have a class with definition you could use __init__, that definition will always get executed when you run the class.
2) A definition always needs a 'self'.
3) booksread is defined in main(), so the other definitions don't know of its existence. So if you put it in nestediffunction() then it also know how it looks like and what value belongs wit it.

Edit:
I rewrote you excersice if you don't mind.
I made it a little more simple, that will show you a bit more clear inhertence works.

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

Comments

0

You could follow the above answers which are absolutely correct of you could slightly change your code structure to this:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.