Fix my program (SOLVED) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Fix my program (SOLVED)

Hello! I have recently made a program which tells whether a number is mathematically "happy" or "sad". It has some errors though. (SOLVED) 1. The "break" command is not working, causing the program to spam text. (SOLVED) 2. No matter what do you insert, you always get "sad", atleast 100 times. The number is possibly being squared whole rather than its digits. Please make sure to read the description of the program to understand how it's supposed to work. Looking forward to your suggestions. https://code.sololearn.com/cBo90Am5H5Aq/?ref=app

12th Jan 2023, 8:13 AM
tnaa
tnaa - avatar
6 Answers
+ 6
tnaa , as already said from people in other posts, the current code can be difficulf to clearly understand how it works. also debugging is not ideal. > the code should be stripped down to smaller parts that have a good readability > we can create a function that is doing the calculation (squaring and summing). functions have the advantage that they can be tested independently from the rest of the code. > i have tried to keep your first ideas as good as possible https://code.sololearn.com/ciOS9hvMGlzH/?ref=app
13th Jan 2023, 3:17 PM
Lothar
Lothar - avatar
+ 5
tnaa , > there are several issues with this code. one of them is: ... templist = [] for i in range(0, len(templist)): g = templist[i]**2 p += g ... this *for loop* that should do the core part of the calculation will never be used, since just before this loop the list *templist* is initialized and empty. the loop condition is a range object that starts with *0*, and ends also with *0* since *len(templist)* returns *0*. > try to solve this problem. also use meaningful variable names which makes debugging and reading easier. > another issue that can happen if the number to process will get the value of 4. in this case the following results are looking like: 4 16 37 58 89 145 42 20 4 ... which is an infinite loop... ^ ^ so we have to decide what to do in this case.
12th Jan 2023, 11:36 AM
Lothar
Lothar - avatar
+ 3
1) your loop condition is forceloop > 0: Initially true, until it is true, loop runs. So after printing, decrement forceloop like : print("sad") forceloop-=1 break 2) you are adding n to usednum list first. then first iteration s=usednum[0] which is n. and s==n is true. so prints sad. first for loop won't run because templist is empty and newnum==1 is false so it goes to 2nd loop. for n => make n squire. add individual digits to list. find each digit squires and pop from list. and add sum of digit squires to list.. if list has single digit , stop loop if it is 1 print happy otherwise print sad. actually, your program prints happy only if input is 1. no other cases. because you are not making changes for newnum value..
12th Jan 2023, 8:41 AM
Jayakrishna 🇮🇳
+ 2
tnaa It's better not to change the original question or code.. Now, you code does is : add number n to usednum list 2 times. Then s == num which is both input number only, comes true and prints "sad" 2 times, and forceloop decremented 2 times so forceloop>0 false, loop exists.. Instead for infinite loop, use : while True : and break loop when squire result is single digit like squire<10 Right now, this code some difficult to read. You made complicated, not necessarily I think... Instead of complicating this task, first try implement different function for finding squire, making individual digits of list, pop-ing list, adding to list.. Then you can just call this functions in loop with 4 steps only. So you can find easily where the problem is occuring.... If you are sure about your process then, to continue loop, make forceloop = 5 #, instead of 1
13th Jan 2023, 8:57 AM
Jayakrishna 🇮🇳
+ 2
Thanks for all your suggestions. The program is now working as intended :D I am now closing the topic about fixing the program, but if you still have some other things to say or ask for, feel free to do it. Thanks again! To be honest, I forgot that functions exist...
13th Jan 2023, 5:31 PM
tnaa
tnaa - avatar
+ 1
Some changes have been recently inserted into the program. For example, there's a solution on how to separate digits from one another. Also, every loop should now be used. However, if your input isn't equal to 1, it always outputs sad with 2 proccesses (attempts) (this has been also added). Meanwhile for an input of 7 it should show 5 attempts and "happy" string. Any further suggestions?
12th Jan 2023, 4:40 PM
tnaa
tnaa - avatar