I am using following code to find sum of digits in Python but infinite loop gets started as I run program
def digit_sum(n):
k=str(n)
i=0
while i<range(len(k)):
l=int(i)
j=0
j=j+i
print j
i+=1
digit_sum(1234)
You have an indentation error.
So, for getting correct output try this way . You can check this code here .
def digit_sum(n):
k = str(n)
i = 0
j = 0
while i < len(k):
l = int(k[i])
j = j + l
i += 1
print j
digit_sum(1234)
The indentation is wrong. The while loop is outside of your function. Indent it to stay inside the function. Also give more meaningful names to your variables.
Its looks like you are new to python So i m going to help you out i have seen you code it seems like you have indentation problem and some logic problem also so i have updated it see it here
def digit_sum(n):
k = str(n)
j = 0 #sum for digits
i = 0
while i in range(len(k)):
#Add convert string to the int and add to the sum
j = j + int(k[i]);
#increment Counter
i = i+1
print j # print Sum
digit_sum(1234)
For more info about indentation you can See Here
Related
this code is to find a subset of a string, which has unique characters.
make k parts of the string and t then print these parts while not includeing the same character in the string
def merge_the_tools(string, k):
l = len(string)
start = 0
t = []
ans = []
while True:
put=''
for i in range(start,start+k):
put += string[i]
start = k
t.append(put)
if start + k == l:
break
for i in t:
ss = set(i)
s = ''
for j in ss:
s += j
ans.append(s)
for i in s:
print(I)
merge_the_tools('AABBAACC',2)
I think there is a problem with ,while loop which I'm not able to find.
Your need is not clear and I will respond with my understanding of your code.
you need to change the value of your start or k because here you will be stuck in start = 2 and k = 2.
Change the line
Start = k
by
start += k
it should work and stop running for infinite time.
I am new to python and trying to write a program to add all the numbers starting from 1 through n and print their sum. Can anyone please tell me what is wrong with my code. I am getting 1 as an output.
def problem1_3(num):
sum_= 0
num = int(num)
for i in range(1,num):
sum_ = sum_ + i
print(sum_)
i+=1
return(sum_)
You are returning the sum_ on the first iteration of the function. You want to finish the for loop, and only then return it. Read more about loops in python, they are based on indentation, unlike some other languages.
Two mistakes that you are making in your code:
1- You are incrementing i by +1 but you don't have to do this because python will automatically increment loop variable.
2- You are returning your sum_ variable inside for loop that's why you got 1 as an output.
def problem1_3(num):
sum_= 0
num = int(num)
for i in range(1,num+1):
sum_ = sum_ + i
#print(sum_)
# i+=1
return(sum_)
ans = problem1_3(5)
print(ans)
Output:
15
So you are returning inside your for loop, so it does not go to completion.
but if you would like an easier more concise way to write that function,
def problem1_3(num):
return sum(range(num)) #for up to, but not including num
or
def problem1_3(num):
return sum(range(num+1)) #for up to, AND including num
Since you already have the range function, range produces a list of numbers. So range(5) is [0,1,2,3,4]
and summing them will get you 1+2+3+4
it's just a shorter way of doing the same thing.
If you don't have to use for loop, it's better to use recursion.
def problem1_3(num : int) -> int:
if num == 1:
return 1
else:
return num + problem1_3(num-1)
# 5 + problem1_3(4)
# 5 + 4 + problem1_3(3)
# 5 + 4 + 3 + problem1_3(2)
# 5 + 4 + 3 + 2 + problem1_3(1)
# 5 + 4 + 3 + 2 + 1
.
if you have to use loops.
your problem is when you are returning sum_. you should to finish your loop and after all, Outside the loop, return the sum:
def problem1_3(num : int) -> int:
sum_ = 0
for i in range(1, num+1):
sum_ += i
return (sum_)
for l in range(1,len(S)-1):
for i in range(1,len(S)-l):
j=i+l
for X in N:
max_score = 0
args = None
if j==2:
print j
for s in range(i,j-1):#s is for split point
if j==2:
print j
for W in probBiNonterDic.keys():#y<==> X->YZ
if j==2:
print j
as you see that in first for block of X if j==2: print 2 but when in second block of s and W, if j==2 does not print 2...why is it, changing j value
Because both the l and i loop start a 1 and j = i + l. At this point j == 2 and prints out. When you try and loop from 1 to 1 in the s loop you actually don't loop even a single iteration and by the next time you get to the s loop j no longer equals 2.
>>> for x in xrange(1,1):
... if True:
... print "hi"
>>>
It looks like your problem is with the second for loop. You are trying to loop over a range that goes from i --> j -1, but because you increase the value of j at the beginning, this is the same as writing range(i, i). Perhaps you could increment j at the bottom of the for i in range(1, len(s) - 1) loop.
I'm having trouble filling out a question on an online python tutorial. It seems really simple but for the life of me I can't figure it out. This is the problem "write a for loop that adds all the numbers 1 to 10 and returns the sum." And this is the code I have been trying:
def run():
sum = 0
for i in range(11):
sum += i
return sum
What am I doing wrong? Thanks for any help.
You're returning within the loop, after one iteration. You need to dedent the return statement so that it falls outside the loop:
def run():
sum_ = 0
for i in range(11):
sum_ += i
return sum_
if anyone want to know how to add 0 + 1 count until 100. There is it!
x = 0
while x<100:
x += 1
print(x)
You are returning the sum from within the for loop. Indent it outside. Keep it at the same level of indentation as for.
You need to dedent the return statement so that it falls outside the loop:
def addNumbers(num)
sum=0
for i in range(0,num+1)
sum=sum+i
return sum
def run(n):
total = 0
for item in range(n):
total = total + item
return total
print(run(11))
Convert the following while loop into a for loop. Your code should be written so that the return statement does not need to be altered. You are allowed to change the code before the while loop proper. string_list is a list of string that contains at least 15 instances of the string '***'
j=0
while i<15:
if string_list[j] ='***':
i +=1
j +=1
return j
I tried doing this first it says while i<15 and i = i+1 so i know i ranges from (0...15) not including 15 so basically we will have like i = range(15) so we will have something like:
for i in range(15):
I don't know what that j is doing there.
I will assume you meant to write string_list[j] == '***' instead of string_list[j] ='***'. I will also assume i is initialised to 0.
i, j = 0, 0
while i < 15:
if string_list[j] == '***':
i += 1
j += 1
return j
The first step is to understand what the loop is actually doing. It is iterating through string_list elements, and every time an '***' is encountered, i is incremented. The loop will terminate when i reaches 15, and since a precondition was given that string_list contains at least 15 copies of '***' we do expect the loop should terminate.
When the loop terminates, i will be equal to 15 and j will simply have counted the number of elements in the list up until this point.
So to start you off, you could try iterating like this:
for s in string_list:
if s == `***`:
# keep track of how many times this has happened
# break if you've seen it happen 15 times
j += 1
return j
j = [i for i,x in enumerate(string_list) if x=='***'][15]
return j
lazy version inspired by comments from Peter and astynax
from itertools import islice
j = next(islice((i for i,x in enumerate(string_list) if x=='***'), 15, None))
return j