Why this Python code does not run؟ | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren
+ 5

Why this Python code does not run؟

https://code.sololearn.com/cqPsx2lYZAlh/?ref=app Hello friends, can you help me why my code does not run? I wrote the output sample I want but it does not

30th Mar 2022, 7:05 PM
Shadow
Shadow - avatar
24 Antworten
+ 8
# read comments for errors.. n = int{input()} #wrong braces {}, use () list = [] i=int (-1) #no need int() while (n): list.append(int(n%10)) #idenatation missing so infinite loop now n = int ( n/10) # i+=1 # # above 3 statements should be idented into while block temp = list [ 0 ] while (1) : #always , no stop condition is added , it must be i>0 temp = int ( list [i] ) + temp #identation missing i-=1 if temp > 10 : n ** temp #add the above 4 lines into loop return 5 # invalid statement here, remove this print(temp) # add proper identation and make changes as mentioned. hope it helps.
30th Mar 2022, 7:15 PM
Jayakrishna 🇮🇳
+ 3
Shadow Read my reply carefully and notice what I wrote about indentation.
30th Mar 2022, 7:26 PM
Lisa
Lisa - avatar
+ 2
line 1: it is int(), not int{} Beyond that: the indentation is completely messed up (not existent)
30th Mar 2022, 7:15 PM
Lisa
Lisa - avatar
+ 2
# my code is working as you want # I hope that will help you # check your indentation n = int(input()) def main(n): list = [] i =-1 while(n): list.append(n % 10) n = n // 10 i +=1 ans = list[0] while(i) : ans = int ( list [i] ) + ans i-=1 if ans >= 10 : return main(ans) print(ans) main(n)
31st Mar 2022, 7:39 PM
Hamed
Hamed - avatar
+ 2
No problem Happy to help Default function names can not be the var name Like => list / print / def / &... If you use them the program thinks that is a function not a var Thats why
1st Apr 2022, 7:33 AM
Ardavan Eskandari
Ardavan Eskandari - avatar
+ 1
Int () is the correct form my dude 🤗
1st Apr 2022, 7:06 AM
Ardavan Eskandari
Ardavan Eskandari - avatar
+ 1
The next error is in line 6 Maybe changing list variable's name could help
1st Apr 2022, 7:08 AM
Ardavan Eskandari
Ardavan Eskandari - avatar
+ 1
1st Apr 2022, 7:27 AM
Shadow
Shadow - avatar
+ 1
#here is shorter and cleaner version #renamed list variable to l as "list" is python command #removed second loop as you can calculate list sum using sum() function n = int(input()) def main(n): l = [] while(n): l.append(n % 10) n //= 10 ans = sum(l) if ans >= 10 : return main(ans) print(ans) main(n)
1st Apr 2022, 7:52 AM
Alan Gregorić
Alan Gregorić - avatar
+ 1
You should refer to the syntax and indentation in python to solve the error
1st Apr 2022, 2:03 PM
Penumudi Lavanya
Penumudi Lavanya - avatar
1st Apr 2022, 2:36 PM
Shadow
Shadow - avatar
0
#corrected code n = int(input()) list = [] i= -1 while(n) : list.append(n%10) n = n/10 i+=1 temp = list [ 0 ] while(i) : temp = int ( list [i] ) + temp i-=1 if temp > 10 : n ** temp print(temp) #read comments above, and compare with your code. spaces at beginning are adding identation to make a same block of code
30th Mar 2022, 7:25 PM
Jayakrishna 🇮🇳
0
Jayakrishna🇮🇳 Thank you . But I do not need to write a riturn? Can you use this way to tell why you did not write? thanks again
30th Mar 2022, 7:28 PM
Shadow
Shadow - avatar
0
#Ok. Sending failed. So adding here. #Is this you are trying? return statement is used only in a function. n = int(input()) def func(n): list = [] i=-1 while(n): list.append(n%10) n = n//10 i+=1 temp = list[0] while(i) : temp = int ( list [i] ) + temp i-=1 if temp >= 10 : return func(temp) # repeated this call until temp is less than 10 print(temp) func(n) """ edit: Shadow you can do same in simple way by using inbuilt sum function. https://code.sololearn.com/cJOAxW8R3tA0/?ref=app hope it helps.. """
30th Mar 2022, 8:16 PM
Jayakrishna 🇮🇳
0
Shadow Everything's Fine?
1st Apr 2022, 7:29 AM
Ardavan Eskandari
Ardavan Eskandari - avatar
0
Ardavan Eskandari I realized. Thankful
1st Apr 2022, 7:35 AM
Shadow
Shadow - avatar
0
Shadow Anytime !
1st Apr 2022, 7:37 AM
Ardavan Eskandari
Ardavan Eskandari - avatar
0
I would suggest you, use isourcecode editor for such silly mistakes.
1st Apr 2022, 9:21 AM
Sachin Kumar
Sachin Kumar - avatar
- 1
Lisa Thanks, I changed line one, but it gave an error again
30th Mar 2022, 7:25 PM
Shadow
Shadow - avatar
- 1
Hello
31st Mar 2022, 6:20 PM
Jaswanth Reddy