Bottom Page

• 3 Vote(s) - 2.33 Average
• 1
• 2
• 3
• 4
• 5
 3 random numbers MrGoat Programmer named Tim Posts: 18 Threads: 6 Joined: Jan 2019 Reputation: 0 Likes received: 1 #1 Jan-15-2019, 05:28 AM (This post was last modified: Jan-15-2019, 06:10 AM by Larz60+. Edited 2 times in total. Edit Reason: added code tags ) So I was given this question : Pick any 3 random ascending numbers and write out a loop function that prints out all 3 numbers. This was the code and solution presented to me. Can anyone understand it and explain it to me please ? I have looked at it but cannot see how it was derived and why the correct solution was printed. Thanks alot ! ```# any 3 ascending numbers , counter must start at 0. # 400 467 851 i = 0 x = 400 while x < 852: print(x) if i > 0: x = x + ((i + 4) * 67) + (i * 49) else: x = x + 67 i = i + 1``` Larz60+ wrote Jan-15-2019, 06:10 AM:Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button. Added for you this time, please use on future posts You like this post Larz60+ aetate et sapientia Posts: 9,042 Threads: 325 Joined: Sep 2016 Reputation: 388 Likes received: 1181 #2 Jan-15-2019, 06:33 AM line 5: everything below x = 400 will be executed until x >= 852 on 1st iteration 1 = 0 and x = 400, so while loop is executed 6 of course prints value of x (400) line 7 says if i > 0 execute line 8 otherwise execute line 10 -- 1st iteration is 0, so else gets executed line 10: x = x + 67 -- so x becomes 467 line 11 increments i by 1 -- i becomes 1 --- back to line 5 -- x is 467 so still less than 852 line 6 print x = 467 line 7 this time i is greater than 0, so execute line 8 line 8 can be broken down: x = 467 = ((i + 4) * 67) = x + (4 * 67) = 467 + 268 = 755 x = 755 + (i * 49) = 755 + (1 * 49) = 755 + 49 = 804 repeat one more cycle (x < 852) for final results You like this post « Next Oldest | Next Newest »

Top Page

 Possibly Related Threads... Thread Author Replies Views Last Post output a list of random numbers 'x' columns wide adityavpratap 4 218 Jan-13-2020, 05:32 PM Last Post: perfringo Can i prevent the random generator to generate already used numbers? MauserMan 3 274 Jan-05-2020, 04:44 PM Last Post: MauserMan Working with Random Generated Numbers YoungGrassHopper 4 405 Sep-10-2019, 06:53 AM Last Post: YoungGrassHopper Need to generate random numbers Gateux 8 559 Jul-19-2019, 03:37 PM Last Post: Man_from_India Generate unique random numbers from different lists Takeshio 5 594 May-24-2019, 07:29 PM Last Post: ichabod801 Print Numbers starting at 1 vertically with separator for output numbers Pleiades 3 510 May-09-2019, 12:19 PM Last Post: Pleiades from 50 random numbers printing only the sqrt(...)==0 AFKManager 1 521 Jan-12-2019, 08:16 PM Last Post: ichabod801 20 x 20 Integer array with random numbers harryk 3 759 Jul-28-2018, 03:38 PM Last Post: harryk

Forum Jump:

Users browsing this thread: 1 Guest(s)