Bottom Page

• 0 Vote(s) - 0 Average
• 1
• 2
• 3
• 4
• 5
 [split] The Collatz Sequence valencia Unladen Swallow Posts: 3 Threads: 0 Joined: Jan 2019 Reputation: 0 Likes received: 0 #1 Jan-06-2019, 06:01 AM Hello, This is my first time posting. Hope my question is clear and thank you in advance for everyone's advise. I'm newbie to python and never expose to any programming whatsoever. Took a while to figure out this collatz exercise - based on Automate the Boring Stuff (ABS). Questions: 1.The code below gives me the result I want - based on what the output should be per ABS and after doing many hours of research on how others solve this (and tweak it to my style). When the code prompt me to 'enter a number', I type 3 and i get the expected output. However, I don't understand why the WHILE statements has to be !=1? Since I type 3 anyway (assume user only type 3), I tried to change the statement to >2 or =3, but it gives me different result. I'm very confused. 2. Is it possible to use FOR loop instead of WHILE loop? Try to do it myself, (just replace WHILE with FOR), and I got error. I also can't seem to find anyone else who use FOR loop in this exercise. 3. What's the difference between print and return? I thought I was just duplicating the code until I accidentally switch the order of print/return (i.e.return THEN print, instead of print THEN return), and the code doesn't work. Tried to do my own google research but i don't see anything very clear. Maybe it will help if you can please advise based on my code below (so i can relate to it)? Again, thank you kindly. ```print('enter a number') def collatz(number): if int(number) %2==0: print(int(number)//2) return(int(number)//2) else: print(3*int(number)+1) return(3*int(number)+1) number=input() [b]while int(number)!=1:[/b] number=collatz(number) ``` Gribouillis Posts: 1,436 Threads: 8 Joined: Jan 2018 Reputation: 128 Likes received: 343 #2 Jan-06-2019, 10:23 AM (This post was last modified: Jan-06-2019, 10:23 AM by Gribouillis. Edited 6 times in total.) You don't need to convert 'number' to integer every time you're using it. You only need to convert the user input to integer once when it is read ```def collatz(number): if number %2==0: print(number//2) return(number//2) else: print(3*number+1) return(3*number+1) number=input('enter a number ') number = int(number) while number!=1: number=collatz(number)```valencia Wrote:I don't understand why the WHILE statements has to be !=1? This is based on the conjecture that the Collatz sequence eventually becomes 1 whatever the initial value. We want to stop the program once it reaches this value. If you replace !=1 by >2 then the program will stop if number is equal to 1 or 2. If you replace by !=3, the program may never stop, for example the sequence 8 -> 4 -> 2 -> 1 -> 4… never reaches 3. valencia Wrote:Is it possible to use FOR loop instead of WHILE loop? The for loop is used to iterate over the elements of a python sequence such as a list, a tuple or any other iterable object. The syntax explicitly needs such an object: `for number in some_sequence`. You can't simply replace while by for, you need a python sequence. It can be done here by using a more advanced code that creates the Collatz sequence as a python object with the `yield` keyword ```def collatz_sequence(number): if number == 1: return elif number %2==0: number = (number//2) else: number = (3*number+1) yield number number=input('enter a number ') number = int(number) for number in collatz_sequence(number): print(number)```valencia Wrote:What's the difference between print and return? `print()` sends information to the user by writing something in the console while `return` sends information to the program by terminating the current function's execution and returning a value that can be used at the place where the function was called. In your code this is line 13. The value returned by collatz() is used as the value in number=value at line 13. valencia Unladen Swallow Posts: 3 Threads: 0 Joined: Jan 2019 Reputation: 0 Likes received: 0 #3 Jan-06-2019, 01:39 PM thank you kindly for the explanation. it's clear now. But the FOR function is not very clear. The suggested code using FOR do not return the result of the original code (using WHILE). when i type 3, it only return 10 (instead of returning a sequence of number). Does it mean it is not possible using FOR? ```def collatz_sequence(number): if number == 1: return elif number %2==0: number = (number//2) else: number = (3*number+1) yield number number=input('enter a number ') number = int(number) for number in collatz_sequence(number): print(number) ``` Gribouillis Posts: 1,436 Threads: 8 Joined: Jan 2018 Reputation: 128 Likes received: 343 #4 Jan-06-2019, 01:47 PM Yes sorry I made a mistake, it should be ```def collatz_sequence(number): while number != 1: if number %2==0: number = (number//2) else: number = (3*number+1) yield number ``` valencia Unladen Swallow Posts: 3 Threads: 0 Joined: Jan 2019 Reputation: 0 Likes received: 0 #5 Jan-06-2019, 08:10 PM Thank you. Now i know FOR is not doable for this case. « Next Oldest | Next Newest »

Top Page

 Possibly Related Threads... Thread Author Replies Views Last Post Fibonacci sequence problem is confusing me adam10e10 2 140 Apr-04-2019, 10:55 AM Last Post: perfringo Creating a list from a sequence without while-loop schniefen 2 134 Apr-01-2019, 07:40 PM Last Post: nilamo The Collatz Sequence Truman 11 2,072 Dec-27-2018, 11:28 PM Last Post: Truman Basic Python Program for Sequence patycanes 7 3,403 Jul-15-2018, 08:53 PM Last Post: ichabod801 ValueError: setting an array element with a sequence ying9309 2 2,893 Jul-15-2018, 07:04 AM Last Post: ying9309 TypeError: can't multiply sequence by non-int of type 'complex' FeverDream 2 1,298 Jul-12-2018, 11:08 PM Last Post: DeaD_EyE [split] [split] regular expression kerzol81 1 561 Jun-24-2018, 09:18 AM Last Post: volcano63 TypeError: can't multiply sequence by non-int of type 'str' rmpadilla73 4 11,356 May-28-2018, 12:21 AM Last Post: py_learner Linear Fibonacci sequence Tawnwen 1 739 Feb-27-2018, 08:01 AM Last Post: nilamo TypeError: can't multiply sequence by non-int of type 'str' Beatenberg 12 4,767 Oct-10-2017, 10:14 PM Last Post: Beatenberg

Forum Jump:

Users browsing this thread: 1 Guest(s)