+ 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
24 Respostas
+ 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.
+ 3
Shadow Read my reply carefully and notice what I wrote about indentation.
+ 2
line 1: it is int(), not int{}
Beyond that: the indentation is completely messed up (not existent)
+ 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)
+ 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
+ 1
Int () is the correct form my dude 🤗
+ 1
The next error is in line 6
Maybe changing list variable's name could help
+ 1
Ardavan Eskandari Hamed thx🌟🙏
+ 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)
+ 1
You should refer to the syntax and indentation in python to solve the error
+ 1
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
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
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..
"""
0
Shadow Everything's Fine?
0
Ardavan Eskandari I realized. Thankful
0
Shadow Anytime !
0
I would suggest you, use isourcecode editor for such silly mistakes.
- 1
Lisa Thanks, I changed line one, but it gave an error again
- 1
Hello