Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
The Collatz Sequence
#11
(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 + 1
and then print the result inside your while loop.


Bro, I am new to python.
can you please tell why are we using try and except in this code
Reply
#12
(Dec-27-2018, 07:19 AM)s_sahil Wrote: Bro, I am new to python.
can you please tell why are we using try and except in this code

Because we assume that some error may appear and when it happens we should receive a clear message. In this case if a user receives a value error he didn't add a number and that's what we want him to know.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Collatz-problem CaptainNeemo 2 2,158 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,695 Nov-23-2020, 05:15 PM
Last Post: cananb
  [split] The Collatz Sequence valencia 4 3,628 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