Determining if a number is a palindrome [closed] - python

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
#WAP to check given number is Armstrong or not, (done)
#if it is Armstrong then print reverse of that number, (done)
#if it is not Armstrong then check it is Palindrome or not. (problem)
no=int(input("Enter your number:"))
temp=no
arm=0
rev=0
while(no>0):
rem=no%10
cube=rem*rem*rem
arm=arm+cube
no=no//10
if(temp==arm):
while (temp> 0):
rem = temp % 10
rev = (rev * 10) + rem
temp = temp // 10
print("Reverse is:", rev)
elif(temp!=arm):
while (temp > 0):
rem = temp % 10
rev = rev * 10 + rem
temp = temp // 10
if(rev==temp):
print("It's a palindrome.")
else:
print("It's not a palindrome.")
I can't find out the problem with the "check if it is a palindrome" part.

In your code to check for palindrome, you are repeatedly dividing your temp value by 10, but your condition is for temp>0 which will never be reached, as repeated division by 10 will never result in a negative number. So, you should change your condition to while(temp>=1).
Also, you should compare the final value of rev to no instead of with temp.
So if you change your final condition to if(rev==no): it should work. This is because your temp keeps getting modified in your loop to check for palindrome, whereas you want to compare your final rev with the original number.

Try this to check if number is palindrome or not
if str(temp)==str(temp)[::-1]:
print("Number is palindrome")
else:
print("Not palindrome")

Related

How can I print digits at even indexes of a number? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Let's say x is an integer number. How can I print every even digit of this number?
For example, if the input is 34567910, then I want the output to be 4 6 9 0.
I know how to print every single digit using the following code, but I can't figure out how to print even digits only:
for i in str(x):
print(i)
Here is the solution to your problem. Note that I have to check if i is odd, due to Python indexes starting from 0. For example, in Python, an index of 1 is the second position.
num = input("Enter a number: ")
return_num = ""
# Iterates through input
for i in range(len(num)):
# Checks if digit is at even position
if i % 2 == 1:
# If so, adds it to return_num
return_num += num[i] # + " " if you want spaces between numbers
print(return_num) # Prints 4690 with your input
Alternatively, you could achieve this using one for-loop. (Credit to OneCricketeer.)
num = input("Enter a number: ")
return_num = ""
# Iterates through specified indexes of input
for i in range(1, len(num), 2):
return_num += num[i] # + " " if you want spaces between numbers
print(return_num) # Prints 4690 with your input
Or, if you want to have the shortest program humanly possible to solve your problem (Credit to Tomerikoo):
num = input("Enter a number: ")
print(num[1::2]) # Prints 4690 with your input
This is one way how you can do it
# iterate over string
x = 34567910
string = str(x)
for index in range(len(string)):
# check if index is divisible by 2
if index % 2 != 0:
# print character at index
print(string[index], end = '')
x = str(input())
Try using slicing and map method:
list(map(int, list(x)[1::2]))
[4, 6, 9, 0]

Program that prompts the user for a non-negative integer n, and then writes a sum of the even digits of n [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I want a program that sums the even numbers of a bigger number using while function.
Exemple :
Number is : 12345
Print : 6 (2+4)
This is what i wrote so far:
num = int(input("Introduce a non negative number: "))
if num % 2 == 0:
sum += num
print("sum")
I can't stress this enough
When doing school assignments, the whole idea with assignments is to teach you concepts, not solutions. There's a reason why you were given this assignment, asking others to solve it for you - is not the way to go.
Go back to your teacher, and ask for help if something is unclear. But because others start posting solutions I might as well just keep mine here.
Skipping the conversion to an early integer, will allow you to iterate over it as a string, and grab one number a a time.
num = input("Introduce a non negative number: ")
total = 0
for i in num:
if int(i) % 2 == 0:
total += int(i)
print("sum:", total)
You can then use your original logic, with some minor modifications.
Since for whatever reason, you're only allowed to use while and not for, you'd have to just adapt a bit.
num = input("Introduce a non negative number: ")
total = 0
i = 0
while i < len(num):
if int(num[i]) % 2 == 0:
total += int(num[i])
i += 1
print("sum:", total)
While I'm at it, after reading my code again. I am quite sure that a while loop here is the least pretty solution to this problem. But from a teaching standpoint there might be some benefit here. But I'd recommend going with the for loop if at all possible.
Try this one:
# split the number to list of digits
digits_list = list(input("Introduce a non negative number: "))
total = 0
# loop over the digits list and pick a digit till the list is empty
while digits_list:
digit = digits_list.pop()
if int(digit) % 2 == 0:
total += int(digit)
print(total)

Looping a whole python function until true [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I'm new to python and I want to archive a function as below where a input of a user gets checked with the arithmetic operators against the case of the random typed number being true to the statement number < 100 and when not being looped by the elif function.
So I want to check the first if statement but if that is not true it should go to the elif statement and then be checked by the if statement again until it fits the criteria.
def unit(number):
if number < 100:
a = round(((number/10)-(number//10))*10)
return a
elif number => 100:
number/10
userInput = int(input("Your number please: \n"))
print(unit(userInput))
SOLVED!
I could solve the problem by doing the following changes:
userInput = int(input("Your number please: \n"))
def unit(number):
if number < 100:
a = round(((number/10)-(number//10))*10)
return a
elif number >= 100:
b = (number/10)
return unit(b)
print(unit(userInput))
use recursion
def unit(number):
if number < 100:
a = round(((number/10)-(number//10))*10)
return a
elif number => 100:
return unit(number/10)
userInput = int(input("Your number please: \n"))
print(unit(userInput))

intro to python - series of numbers [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I have been going through an introduction to python booklet and have been stuck on the following question. the question is outlined below and my attempt follows after the question.
Take this program:
m = 0
finished = False
while not finished:
print('Enter another whole number (0 to finish): ', end = '')
s = input()
num = int(s)
if num != 0:
if num > m:
m = num
else:
finished = True
print(str(m))
If you have worked out what the above program does, can you see that,
for certain series
of numbers, it will not produce the correct output? In what
circumstances will it not
work correctly, and how could you change
the program to make it work properly?
My understanding is that the series of numbers, where the above program will fail, are decimal numbers (non-whole number), therefore my attempt is as follows:
m='0'
finished = False
while not finished:
print('enter number, 0 to finish: ', end = '')
num = input()
if num != '0':
if num > m:
m = num
else:
finished = True
print(m)
However this fails at understanding that 77 is larger than 8 since it is reading it as a string.
This program calculates the maximum value from the inputted sequence. It stores the maximal value in m, and if a num is inputted that's larger than it, it keeps it as the new maximum.
However, note that m is initialized with 0, making the implicit assumption that at least one number you'll input is positive. If you input only negative numbers, you'll get 0 as the largest number, which is clearly wrong, as you never inputted it.
A quick fix could be to initialize m with None and explicitly check for it:
m = None
finished = False
while not finished:
print('Enter another whole number (0 to finish): ', end = '')
s = input()
num = int(s)
if num != 0:
if not m or num > m:
m = num
else:
finished = True
print(str(m))

Python dice game [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I must create a dice game that generates numbers from 1 to 6. It will then throw the dice 50 times and it will count the number of odd numbers and even numbers. I'm using Python.
Here is my code:
import random
# Determine odd and even numbers
throws = 0
even = 0
odd = 0
maxthrows = 50
print "Even : Odd"
while True:
throws += 1
if throws == maxthrows:
break
dice = random.randrange(6)
if dice % 2 == 1:
odd += 1
else:
even += 1
print even, " : ", odd
raw_input("Press enter to exit.")
Your loop is wrong, it should be:
while throws != maxthrows:
throws += 1
dice = random.randrange(6)
if dice % 2 == 1:
odd += 1
else:
even += 1
Notice that:
Whenever possible, the exit condition should be used in the loop condition, not in an if ... break
The part where you ask if the dice is odd must be inside the loop, in Python indentation matters - a lot!

Categories