This question already has answers here:
Python Palindrome
(3 answers)
Closed 2 months ago.
I have tried reversing a given string and then checking it but want to know the actual method to solve such questions without using any built-in functions of python.
Tried using built-in functions in python but expecting to know the code without using them.
z=int(input("Enter number:"))
temp=z
rev=0
while(z>0):
dig=z%10
rev=rev*10+dig
z=z//10
if(temp==rev):
print("The number is a palindrome!")
else:
print("The number isn't a palindrome!")
You can use list slice for this problem
num = 1234
reverse = int(str(num)[::-1])
if num == reverse:
print('Palindrome')
else:
print("Not Palindrome")
int(str(num[::-1]) is nothing but converting the string format to integer .
Related
This question already has answers here:
Determine whether integer is between two other integers
(16 answers)
Closed last month.
if money == (1, 799999):
print("You're a natural!)
I need to make it so, if the money variable is = to anything in between 1 and 799999 it can return a print. But i cant find anything to do it
You can use the following code:
if money>=1 and money<=799999:
print("You are natural.")
Hope it works.....
This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 7 months ago.
a=input("Enter the first Number ")
b=input("Enter the next number ")
c=a>b
print("Is a greater than b ? ", c )
ISSUE is it showing the opposite output always like the when you enter a greater than b it showing flase and vice versa
your problem is that you compare strings instead of floats
since when you are comparing stings python compares the lexicographic value of the strings, that way "9" is grater then "12357645"
if you convert the input to float that should fix it :)
a=input("Enter the first Number ")
b=input("Enter the next number ")
c=float(a)>float(b)
print("Is a greater than b ? ", c )
This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 1 year ago.
a=input("Enter First Number ")
b=input("Enter second number ")
c=a+b
print("The Sum of two numbers are ",c)
"Why This Program not printing Sum of tho number?s?""
You need to cast the numbers to int. When you take an input, it is always a string. To cast a string to int, you can do int(a)+int(b)
This question already has answers here:
How do you input integers using input in Python [duplicate]
(3 answers)
Closed 2 years ago.
whats my mistake?
it doesn't show you are right
print("what's the correct number?")
print("1/2/3/4/5/6/7/8/9")
correctNumber = 6
inputNumber = input()
if inputNumber == correctNumber:
print("you are right")
You can try
correctNumber = 6
inputNumber = int(input("what's the correct number? 1/2/3/4/5/6/7/8/9"))
if inputNumber == correctNumber:
print("you are right")
The problem was that input is store in a variable as a string and you need to cast it to int to compare it with int since correctNumber = 6 is int.
By the way, the input method expects a string to print the user before his input so you don't need to use print statements to notify the user about the expected input.
When using input(), you get a string that you compare int () and str (). to get a number in water you need int(input())
This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 2 years ago.
I'm working through some practice questions in the book "Automate the Boring Stuff," and I'm struggling to understand why the code below only produces 'Greetings!' as output.
print('What does spam equal? Hint: Try a number between 1 and 3')
spam = input()
if spam == 1:
print('Hello')
elif spam == 2:
print('Howdy')
else:
print('Greetings!')
It does because what you have given as input is stored as string in spam . And when you are using if else statements then it is comparing with integers, but your actual value is a string, therefore it always turns out to be in the final else statement printing Greetings!
So use spam=int(input()) instead.
This is because the input() returns a string, which is never equal to an integer.
Try
spam = int(input())
instead. This will of course throw a ValueError if the input is not an integer.