So my program checks for the number of palindrome numbers, lychrels, and non-lychrels. Originally I used a 'break' and a for loop, but I was supposed to use a while loop.
My while loop does not work the same as my for loop and I dont know what i did wrong?
-- The range is meant to be between num1 and num2
also, the output is meant to be an exact copy of the prompt, so thats why it looks like this.
Thanks!
Your while loop:
while (nums>=num1 and nums<=num2 and flag):
#for nums in range(num1, num2+1):
num_str = str(nums)
if num_str == num_str[::-1]:
pal += 1
else:
count = 0
num_el = num_str
while (count < 60):
np_total = int(num_el) + int(num_el [::-1])
count += 1
nums_el = str(np_total)
num_el = nums_el
if num_el == nums_el [::-1]:
nonlych += 1
flag = False
else:
lychrel += 1
print(nums, "looks like a lychrel number")
nums += 1
The
else:
lychrel += 1
print(nums, "looks like a lychrel number")
is executing every time the while loops exits. The break in your for loop was skipping it.
Second problem is that when flag is set to False, it will stop your outer while loop, so the first non-lychrel number you find will be the last number you test.
Here's my attempt at changing as little as possible. You can add a flag like isLychrel instead of using the count variable to pass information.
nums = num1
while (nums>=num1 and nums<=num2):
num_str = str(nums)
if num_str == num_str[::-1]:
pal += 1
else:
count = 0
num_el = num_str
while (count < 60):
np_total = int(num_el) + int(num_el [::-1])
count += 1
nums_el = str(np_total)
num_el = nums_el
if num_el == nums_el [::-1]:
nonlych += 1
count = 999 # breaks while loop
if (count != 999):
lychrel += 1
print(nums, "looks like a lychrel number")
nums += 1
Related
lower = int(input("Enter lower range limit:"))
upper = int(input("Enter upper b range limit:"))
Count = 0
for i in range(lower, upper+1):
if((i%3==0) & (i%5==0)):
Count += 1
print(count)
I want to print 100 values which are divisible by 3 and 5, but in count only 7 is showing, can anyone suggest what to do, it will be appreciated!
Use this
num = int(input("How many Numbers you want :"))
i=0
Count = 0
while(Count != num):
if((i%3==0) and (i%5==0)):
Count += 1
print(i)
i = i + 1
print("Count is :",Count)
Python program to print all the numbers
divisible by 3 and 5 for a given number
Result function with N
def result(N):
# iterate from 0 to N
for num in range(N):
# Short-circuit operator is used
if num % 3 == 0 and num % 5 == 0:
print(str(num) + " ", end = "")
else:
pass
Driver code
if name == "main":
# input goes here
N = 100
# Calling function
result(N)
I couldn't figure out why getting this error though it shows no error if I put count = 1 under the second for loop but not what I expect as count is being set again 1 even though implemented every time just before ending the loop which makes no sense.
I wrote on user input: MF MF (for example)
test_case = int(input())
for x in range(test_case):
my_list = input().split()
count = 1
for each_element in my_list:
if each_element == len(my_list):
if each_element[1] == my_list[0][0]:
temp = 1
break
else:
to_compare = my_list[count]
if each_element[1] == to_compare[0]:
temp = 1
break
else:
temp = 0
count += 1
print("LOOP" if temp == 0 else "NO LOOP")
Supposed to take in a string for example "Maroon 5" and then return only the number in the statement. In this case it's "5" but I just get no output. Thank you!
def findNumbers(str):
found1 = False
found2 = False
i = 0
while not found1 and i<len(str):
if str[i] in "0123456789":
found1 = True
else:
i = i + 1
if found1:
j = 0
while not found2 and j<len(str):
if str[j] in "0123456789":
found2 = False
else:
j = j + 1
else:
return 0
istr = str[i:j]
n = (istr)
return n
print (findNumbers("Maroon 5"))
You have two important errors in your code. One is that once you've found a digit, you jump back to the start of the string:
j = 0
Instead, you want to continue from there:
j = i
The other, bigger problem is that you have an infinite loop. The second loop may continue indefinitely if str[j] is a digit, since setting found2 to False does not end the loop. The code should be
while not found2 and j<len(str):
if str[j] not in "0123456789":
found2 = True
else:
j = j + 1
you can do it like this:
def findNumbers(str):
for i in range(0,len(str)):
if (str[i].isdigit()):
print str[i]
findNumbers("Maroon 5")
It should print the number 5
You can try this way: This will return a list of tuples with the numbers and their indexes
def findNumbers(string):
num = '1234567890'
return ([(string[x], x) for x in range(len(string)) if string[x] in num])
f=0
c=1
n=raw_input("Enter the value of n")
while c<n:
for i in range(2,100):
for j in range(1,i):
if i%j == 0:
f =f+1
if f == 1:
print i
c = c+1
f = 0
If n is 5 then the output should print the first 5 prime numbers.
2,3,5,7,11
You have a few mistakes. You do not parse n as integer, you have an unnecessary loop, you inistailise c with a wrong value.
Here is a corrected version
c = 0
n=int(raw_input("Enter the value of n"))
i = 2
while True:
for j in range(2,i):
if i % j == 0:
break
else:
print i
c = c + 1
if c == n:
break
i += 1
Copy of answer to this question.
You're only checking the value of count at the end of the for loop so you will always end up with the full range of 1-100 being tested. Why is the range limited to 100?
You don't need a for loop at all. Use the while loop to keep finding primes until you have num of them.
Try something like:
import math
def isPrime(num):#this function courtesy of #JinnyCho
if num == 1:
return False
if num % 2 == 0 and num > 2:
return False
for i in range(3, int(math.sqrt(num))+1, 2):
if num % i == 0:
return False
return True
def getPrimes(num):
count = 0
i = 1
highPrime = None
while count < num:
if isPrime(i):
count += 1
yield i
i += 1
n = int(raw_input("Enter the value of n: "))
list(getPrimes(n))
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)