I was trying to achieve this output:
0;12
1;24
2;36
3;48
4;60
... but I got this:
0;12
1;12
2;12
3;12
4;12
Here is the code:
iter = 0
count = 0
letter = 0
for iter in range(5):
while letter < len("hello, world"):
letter+=1
count+=1
print("Iteration " + str(iter) + "; count is: " + str(count))
I managed to fix this by adding count*(iter+1) instead of count in print statement, but what I'm trying to understand is why count variable resets every time when new for iteration begins.
Thanks in advance.
letter is always 13 since it is not reinitialized. So while loop will be executed only once. To fix this,
count = 0
for iter in range(5):
letter = 0
while letter < len("hello, world"):
letter+=1
count+=1
print("Iteration " + str(iter) + "; count is: " + str(count))
What's happening here is you never set letter to 0 after an interation. So letter always stays at 12.
Printing letter like this:
iter = 0
count = 0
letter = 0
for iter in range(5):
while letter < 12:
letter+=1
count+=1
print(letter)
Outputs:
1
2
3
4
5
6
7
8
9
10
11
12
After the first while iteration letter stays at 12 and therefore while letter < 12: is never run so count never increases.
Make sure to set letter to 0 after each while loop:
iter = 0
count = 0
letter = 0
for iter in range(5):
while letter < 12:
letter+=1
count+=1
letter=0
there! this can be fixed by resetting letter to 0 after each while loop. The while loop will only run the first time because, during the second time, the letter value increases to 13 and is no longer < len("hello, world").
iter = 0
count = 0
letter = 0
for iter in range(5):
while letter < len("hello, world"):
letter+=1
count+=1
print("Iteration " + str(iter) + "; count is: " + str(count))
letter=0
Related
I have a string and k = number, which is the length of a substring that has the same letter repeated in a row. How can I have the wanted output only?
The expected output: For length 3, found the substring ddd!
my_string = 'aabadddefggg'
k = 3
x = 1
c = 1
while x < len(my_string):
if my_string\[x\] == my_string\[x-1\]:
c += 1
else:
c = 1
if c == k:
print("For length " + str(k) + ", found the substring " + my_string\[x\] \* k + "!")
break
else:
print("Didn't find a substring of length " + str(k))
break
x += 1
The output:
Didn't find a substring of length 3
Didn't find a substring of length 3
Didn't find a substring of length 3
Didn't find a substring of length 3
Didn't find a substring of length 3
For length 3, found the substring ddd!
get rid of the break after didnt find the string. Your code means that once you evaluate c == k you will always break beacuse you have the break in both the if and the else meaning you can never reach x += 1
my_string = 'aabadddefggg'
k = 3
x = 1
c = 1
while x < len(my_string):
if my_string[x] == my_string[x-1]:
c += 1
else:
c = 1
if c == k:
print("For length " + str(k) + ", found the substring " + my_string[x] * k + "!")
break
else:
print("Didn't find a substring of length " + str(k))
x += 1
OUTPUT
Didn't find a substring of length 3
Didn't find a substring of length 3
Didn't find a substring of length 3
Didn't find a substring of length 3
Didn't find a substring of length 3
For length 3, found the substring ddd!
Even better, put your else statement under the while, this way:
my_string = 'aabadddefggg'
k = 3
x = 1
c = 1
while x < len(my_string):
if my_string[x] == my_string[x-1]:
c += 1
else:
c = 1
if c == k:
print("For length " + str(k) + ", found the substring " + my_string[x] * k + "!")
break
x += 1
else:
print("Didn't find a substring of length " + str(k))
# For length 3, found the substring ddd!
With k = 4 (the else statement is executed if the end of the loop is reached, which happens only if it doesn't break out of it):
# Didn't find a substring of length 4
string = input("Enter the string: ")
sub_string = input("Enter sub string: ")
count = 0
idx = 0
while string.count(sub_string, idx) != 0:
count += string.count(sub_string, idx)
idx = string.index(sub_string, idx)
idx += 1
if string.count(sub_string, idx) == 0:
print(count)
break
when i give this code input as follows:
ininini
ini
It prints output 4. I tried running debugger and found out that it is incrementing count with +2 in the first step instead of +1 and I couldn't figure out that. Any suggestions would be very much helpful.
Use:
count += 1
instead of
count += string.count(sub_string, idx)
string.count(sub_string, idx) is 2 initially, and that's why you end up adding 2 in first iteration (instead of intended 1), thereby getting a 1 more than expected.
What you need is to increment count by 1 in every iteration and if you make this change, you get 3 as output.
I would use a for loop instead:
string = input("Enter the string: ")
sub_string = input("Enter sub string: ")
count = 0
for index in range(len(string) - len(sub_string) + 1):
if string[index: index + len(sub_string)] == sub_string:
count += 1
print(count)
If you are tyring to reach to get the length of the string, just use
len(string)
I was trying to do an exercise, but have a hard time doing it. I need to write a program containing a pair of nested while loops that displays the integer values 1-100, ten per row. I'm not sure how I should use while loops in such a task, I was trying something like this, but it seems like it only gives me first line to 10 and then all other numbers in second line:
i = 1
while i <=100:
print(i, end=' ')
i = i + 1
Any advice?
Hope This will help
#python 2.7.6
for i in range(1, 101):
print(i),
if i%10==0:
print
another solution with nested while loop:
loop1 = 10
loop2 = 10
num = 1
while(loop1>0):
while(loop2>0):
print num,
num = num+1
loop2=loop2-1
print
loop2=10
loop1=loop1-1
Seems like I did the job right, thanks guys for helping me out.
i = 1
per_row = 10
while i <=100:
while(per_row > 0):
print(i, end=' ')
i = i + 1
per_row = per_row - 1
print()
per_row = 10
Here is a bit cleaner one:
i = 1
column = 10
while i <= 100:
if column > 0:
if i < 10:
print(format('', '1') + str(i), end = ' ')
else:
print(i, end = ' ')
i = i + 1
column = column - 1
else:
print()
column = 10
I am trying to solve the 'Love-Letter' mystery problem of HackerRank using Python, but I am stuck at a place where in my loop a variable is not getting updated.
s = input()
first_char = s[0]
last_char = s[-1]
ascii_first_char = ord(first_char)
ascii_last_char = ord(last_char)
count = 0
i = 1
while ascii_first_char < ascii_last_char:
count += abs((ascii_last_char-ascii_first_char))
ascii_first_char = ord(s[i])
ascii_last_char = ord(s[-i])
i += 1
print(count)
If you try to run that, you would see that alc is not changing it's value according to ord(s[i]) where I keeps incrementing. Why is that happening?
You get the first letter with s[0] and the last with s[-1]. In your loop you take the next letters with the same index i.
I don't understand your condition in the while loop. Instead of "ascii_first_char < ascii_last_char" you should test if you have looked at every element of the string. For that we have to loop len(s)/2 times. Something like:
while i < len(s) - i:
or equivalent
while 2*i < len(s):
And this conditions only work for even length. I prefer for-loops when I know how many times I will loop
current_line = input()
# if length is even, we don't care about the letter in the middle
# abcde <-- just need to look for first and last 2 values
# 5 // 2 == 2
half_length = len(current_line) // 2
changes = 0
for i in range(index):
changes += abs(
ord(current_line[i]) - ord(current_line[-(i+1)])
)
print (changes)
s1 = ['abc','abcba','abcd','cba']
for s in s1:
count = 0
i = 0
j = -1
median = len(s)/2
if median == 1:
count += abs(ord(s[0])-ord(s[-1]))
else:
while i < len(s)/2:
count += abs(ord(s[j])-ord(s[i]))
i += 1
j -= 1
print(count)
How can I change the for loop into a while loop. What are the significant differences between using for and while loop?
S="I had a cat named amanda when I was little"
count = 0
for i in S:
if i =="a":
count += 1
print (count)
Following is the while loop implementation of same code.
i = 0
count = 0
while i < len(S):
if S[i] == 'a':
count += 1
i += 1
print count
You need a counter which will be incremented each time "while counter < len(S)"
Here's a start:
index = 0
count = 0
while index < len(S):
#do something with index and S ...
index += 1
You could also do it via the boolean nature of an empty string/list/dictionary.
S="I had a cat named amanda when I was little"
count = 0
while S:
# pop the first character off of the string
ch, S = S[0], S[1:]
if ch == "a":
count += 1
print (count)