-1

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,

Barmar
  • 741,623
  • 53
  • 500
  • 612
Alt_2003
  • 17
  • 3

1 Answers1

3

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)
Samwise
  • 68,105
  • 3
  • 30
  • 44
  • It worked as it was supposed to after applying your solution. But can you please explain what this part do? `c = collataz(c)` – Alt_2003 Apr 14 '21 at 17:22
  • `collataz(c)` calls the `collataz` function with the current value of `c` as its argument. The `=` is the assignment operator; it takes the value on the right (which is the result of calling the function) and assigns it to the name on the left (which is the variable `c`). Each time this line of code executes, it runs `c` through the function and replaces it with the result. – Samwise Apr 14 '21 at 19:12