Search code examples
pythonwhile-looppython-3.9

'int' object has no attribute 'output'


This may be a silly question. I just created a function that divide a number and give remainder if the number is even else do some other mathematical operations in python.

def collataz(number):
    if number % 2 == 0:
        output = number // 2
        return int(output)

    else:
        output = 3 * number + 1
        return int(output)

import sys

try:
    c = collataz(int(input("Enter int")))
    while c.output != 1:
        print(c)
except KeyboardInterrupt:
    sys.exit()

But when I run this code it gives this following error after asking for input 'int' object has no attribute 'output' how can I fix this? and please also tell the reason why my code give this error so I can understand the problem. Please tell if something is not clear.

Thanks,


Solution

  • The name output inside your collatz function does not have any meaning outside of it. The value you return is just an int, which you're assigning to the name c.

    I think you want the last part of your code to be something like:

        c = int(input("Enter int"))
        while c != 1:
            c = collataz(c)
            print(c)