Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
The Collatz Sequence
#1
Write a function named collatz() that has one parameter named number. If number is even, then collatz() should print number // 2 and return this value. If number is odd, then collatz() should print and return 3 * number + 1.
Then write a program that lets the user type in an integer and that keeps calling collatz() on that number until the function returns the value 1.

def collatz(number):
    if number % 2 == 0:
        print(number//2)
    else:    
        result = 3*number + 1
        print(result)        
try:
    n = input("Give me a number: ")
    while n != 1:
        n = collatz(int(n))
except ValueError:
    print('Type a number please!')
message:
Traceback (most recent call last):
File "C:\Python36\kodovi\coll.py", line 10, in <module>
n = collatz(int(n))
TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'


Not sure why am I receiving this message.
Reply


Messages In This Thread
The Collatz Sequence - by Truman - Apr-17-2018, 10:54 PM
RE: The Collatz Sequence - by micseydel - Apr-17-2018, 11:10 PM
RE: The Collatz Sequence - by Truman - Apr-18-2018, 10:11 PM
RE: The Collatz Sequence - by mlieqo - Apr-18-2018, 09:29 AM
RE: The Collatz Sequence - by vaison - Apr-18-2018, 01:41 PM
RE: The Collatz Sequence - by vaison - Apr-19-2018, 11:03 AM
RE: The Collatz Sequence - by Truman - Apr-19-2018, 09:38 PM
RE: The Collatz Sequence - by vaison - Apr-19-2018, 10:23 PM
RE: The Collatz Sequence - by Truman - Apr-19-2018, 10:48 PM
RE: The Collatz Sequence - by mlieqo - Apr-20-2018, 08:08 AM
RE: The Collatz Sequence - by s_sahil - Dec-27-2018, 07:19 AM
RE: The Collatz Sequence - by Truman - Dec-27-2018, 11:28 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Collatz-problem CaptainNeemo 2 2,242 Dec-07-2020, 04:09 PM
Last Post: deanhystad
  How can I found how many numbers are there in a Collatz Sequence that I found? cananb 5 3,835 Nov-23-2020, 05:15 PM
Last Post: cananb
  [split] The Collatz Sequence valencia 4 3,732 Jan-06-2019, 08:10 PM
Last Post: valencia

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020