(Apr-17-2018, 11:10 PM)micseydel Wrote: Is this your full code? How are you running it? Are you providing any input to it? If so, please provide it.
After I run the file in command prompt ( like any other code ) it asks me for a number and after I add it it gives me that error..
(Apr-18-2018, 09:29 AM)mlieqo Wrote: your collatz function is not returning anything so you should change it to something like this:
def collatz(number): if number % 2 == 0: return number//2 else: return 3*number + 1and then print the result inside your while loop.
def collatz(number): if number % 2 == 0: print(number//2) return number//2 else: result = 3*number + 1 print(result) return result try: n = input("Give me a number: ") while n != 1: n = collatz(int(n)) except ValueError: print('Type a number please!')This code works fine after I add return to if/else statement.
vaison, this edit solved the issue with negative integers.
while n != 1: n = collatz(abs(int(n)))