Reputation: 11
I need some help with below:
write a function (main) which will ask the user to enter a starting and ending number range (inclusive). Count through the numbers using a while loop. Add the number into the total only if it is a palindrome (call isNumberPalindrome). Print the total after adding the numbers.'
What I have so far for this (main) function is...
def main():
start = int(input("Enter a number to start counting at:"))
end = int(input("Enter a number to end counting at:"))
while start <= end:
print(start)
start = start + 1
AND here is what I have for my (isNumberPalindrome) function.
def isNumberPalindrome(s):
if len(s) < 1:
return True
else:
if s[0] == s[-1]:
return isNumberPalindrome(s[1:-1])
else:
return False
So far my (main) function asks for user input(starting and ending number) and counts them using a while loop. I have no idea what to add to my code for (main) function next to achieve "Add the number into the total only if it is a palindrome (call isNumberPalindrome). Print the total after adding the numbers."
Thanks for the help.
So far with the code provided to me, this is what happens.
Enter a number to start counting at:1
Enter a number to end counting at:6
1
Traceback (most recent call last):
File "C:/Users/Tyler/Desktop/AWDADXC.py", line 38, in <module>
main()
File "C:/Users/Tyler/Desktop/AWDADXC.py", line 33, in main
if isNumberPalindrome(start):
File "C:/Users/Tyler/Desktop/AWDADXC.py", line 18, in isNumberPalindrome
if len(s) < 1:
TypeError: object of type 'int' has no len()
Does anyone know what is wrong?
Upvotes: 1
Views: 7723
Reputation: 618
Specify the range with the input number
function findPalindromes(number) {
let palindromeCount = 0;
let leftSide = '';
let rightSide = '';
for (let i = 1; i <= number; i++) {
let checkBox = [];
let s = i.toString();
for (let j = 0; j < s.length / 2; j++) {
leftSide = s[j];
rightSide = s[s.length - 1 - j];
if (rightSide === leftSide) {
checkBox.push('y');
} else {
checkBox.push('n');
}
}
if (!checkBox.includes('n') && i > 10) {
console.log(s);
palindromeCount++;
}
}
console.log(`TOTAL: ${palindromeCount} palindromes found`);
}
findPalindromes(1001);
Upvotes: 0
Reputation: 304355
Your problem is assuming that the parameter to isNumberPalindrome
is a str
, but you are passing an int
It's easy enough to check for a palindrome without going converting to a str
. eg.
def isNumberPalindrome(n): # n is an integer
s = n
t = 0
while s:
t = t*10 + s%10
s /= 10
return n == t
Upvotes: 0
Reputation: 685
The problem with your code is that you are calling len
on an integer.
What you should do is call len
on an iterable, e.g. list
or string
(as in the solutions posted in the answers).
For example, and using your recursive implementation of isNumberPalindrome
,
>>> isNumberPalindrome("123321")
True
>>> isNumberPalindrome("HelloolleH")
True
>>> isNumberPalindrome(str(123321))
True
>>> isNumberPalindrome([1,2,3,4,3,2,1])
True
Other than that, if it is not necessary to use your recursive implementation, any of the other answers, should do the work.
Upvotes: 1
Reputation: 30436
Here is a simple stab at this:
def isNumberPalindrome(n):
return str(n) == str(n)[::-1]
filter(isNumberPalindrome, range(10,100))
Outputs:
[11, 22, 33, 44, 55, 66, 77, 88, 99]
To sum them just pass the sequence to sum()
like so:
sum(filter(isNumberPalindrome, range(10,100)))
Outputs:
495
And if you want to factor in user input you can do this:
sum(filter(isNumberPalindrome, range(int(raw_input('Enter a starting number:')), int(raw_input('enter a stopping number:')))))
Which will product the following output:
Enter a starting number:10
enter a stopping number:100
495
Upvotes: 2
Reputation: 414585
To iterate over all integers from start
to end
inclusive:
for n in range(start, end + 1):
print(n)
To get digits; you could use str(n)
:
def is_palindrom(number):
digits = str(number)
return digits == digits[::-1]
where s[::-1]
returns string s
in reverse e.g., "abc"[::-1] == "cba"
.
To find the sum of all palindromes in the range [start, end] inclusive:
total = sum(number for number in range(start, end + 1) if is_palindrom(number))
Upvotes: 1
Reputation: 3504
def main():
start = int(input("Enter a number to start counting at:"))
end = int(input("Enter a number to end counting at:"))
total = 0
while start <= end:
print(start)
if isNumberPalindrome(str(start)):
total += start
start = start + 1
print(total)
Upvotes: 0
Reputation: 20348
You need to have a variable which can store the sum of the palindromes. Than just call an if statement to check if the number is palindrome, if yes add the number into the total or else do nothing.
PS: You can use the code of Juampi provided in the another answer.
Upvotes: 0