This question already has answers here:
If...else statement issue with raw_input on Python
(2 answers)
Closed 7 years ago.
I tried taking an input from keyboard. the checking that input with an if else statement. But everytime the else part is working. The if statement does not happen to be true. I can't understand where I am going wrong.
Here is what I have done.
abc= raw_input("Enter a 2 digit number");
if abc==6:
print "Its party time!!!"
else:
print "Its work time"
Please suggest
Your input variable is a string. You need to cast it to an integer to correctly compare it to 6.
if int(abc) == 6:
raw_input returns a string. abc is a string, and a string will never be equal with an integer. Try casting abc or the return value of raw_input(). Or, you can make 6 a string.
Casting the return value of raw_input() :
abc = int( raw_input('Enter a 2 digit number') )
Casting abc :
abc = int(abc)
or
if int(abc) == 6:
Changing 6 to string :
if abc == '6':
>>> abc= raw_input("Enter a 2 digit number")
Enter a 2 digit number6
>>> if int(abc) == 6:
print "Its party time!!!"
Its party time!!!
>>>
Related
This question already has answers here:
Python input never equals an integer [duplicate]
(5 answers)
Closed 2 years ago.
I'm trying to learn some Python and had a question about a really small "program" I made as a test.
a = input()
print(a)
b = '10'
if a == b:
print("Yes")
else:
print("No")
This works, but my question is why does the value for b have to have the quotes around it.
Python input() function will by default take any input that you give and store it as a string.
why does the value for b have to have the quotes around it
Well, it doesn't have to have quotes. But if you need the condition to evaluate to True, then you need quotes. So, since a is a string, you need to have b = '10' with the quotation mark if you need a == b to evaluate to True.
If your input is an integer, you could also do a = int(input()) and in this case, b=10 would work as well. Simple !
So, the following two can be considered to be equivalent in terms of the result they give -
a = input()
b = '10'
if a == b:
print("Yes")
else:
print("No")
AND
a = int(input())
b = 10
if a == b:
print("Yes")
else:
print("No")
It is actually quite simple. All inputs from the user are considered as string values by python. In python, you can only compare a string to string, integer to integer and so on...
You could do this as well
a = int(input())
print(a)
b = 10
if a == b:
print("Yes")
else:
print("No")
Over here int() converts the value you enter to an integer. So you don't need quotes for the variable b
The value of input() is a string, therefore you must compare it to a string. Everyone learning programming starts off with many questions, and it is the only way to learn, so do not feel bad about it.
In python default input type is string. if you want it as int you must cast it:
a = int(input()) #change type of input from string to int
print(type(a))
b = 10
if a == b:
print("Yes")
else:
print("No")
This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 3 years ago.
I want to make a program where I need to check the input of the user
so, the question is How do I check the input value, if it`s a string or an integer?
Here's some experimental code that didn't work:
a = 10
print(type(a))
if (a==10):
print("aaaaa")
else:
if(type(a)== "<class 'int'>"):
print("12345")
You can do it in the following way:
a = 10
if type(a) is int:
print("Yes")
else:
print("No")
input() will always return string, i.e type(input()) will be str
from my understanding, you want to validate input before proceeding further
, this is what you can do
foo = input()
if not foo.isdigit():
raise ValueError("Enter a valid integer")
foo = int(foo)
this will not work for a negative number, for that you can check if foo startswith "-"
if foo.lstrip("-").isdigit():
multiply_by = -1 if foo.startswith("-") else 1
foo = multiply_by * int(foo.lstrip("-"))
and if you want to check type of variable
a = 10
if isinstace(a, int):
pass
This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 3 years ago.
I am just messing around with Python trying to do a simple guessing program. The code inside my if statement never runs, and I can't figure out why. I've tried printing both variables at the end, and even when they're the same, the comparison never resolves to true. I've also tried just setting y as 2 and guessing 2 as the input, and it still doesn't work.
import random
x = input("Guess a number 1 or 2: ")
y = random.randint(1,2)
if x==y:
print("yes")
The problem here is that x is a string and y is an int:
x = input("Try a number ") # I chose 4 here
x
'4'
x == 4
False
int(x) == 4
True
input will always return a string, which you can convert to int using the int() literal function to convert that string into the required value
This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 7 years ago.
def get_input():
'''
Continually prompt the user for a number, 1,2 or 3 until
the user provides a good input. You will need a type conversion.
:return: The users chosen number as an integer
'''
#pass # REPLACE THIS WITH YOUR CODE
n = input ("Enter the number 1,2 and 3? ")
while n > 0 and n < 4:
print("Invalid Input, give the number between 1 to 3")
n = input ("Enter the number 1,2 or 3? ")
return (n)
get_input()
I am not getting the answer and it's just not working, I am looking for answer like this,
Give me one of 1,2 or 3: sid
Invalid input!
Give me one of 1,2 or 3: 34
Invalid input!
Give me one of 1,2 or 3: -7
Invalid input!
Give me one of 1,2 or 3: 0
Invalid input!
Give me one of 1,2 or 3: 2
Process finished with exit code 0
The input() built-in function returns a value of type str.
As is specified in the (doc)string right after the declaration of function get_input():
You will need a type conversion.
So, you must wrap it in an int() to convert it to an integer int.
n = int(input("Enter the number 1,2 or 3? "))
Then you can use comparison operators to evaluate if it is in the qualified range of accepted values :
# Your comparisons are mixed.
# You can use the in operator which is intuitive and expressive
while n not in [1, 2, 3]:
print("Invalid Input, give the number between 1 to 3")
# remember to wrap it in an int() call again
n = int(input ("Enter the number 1,2 or 3? "))
return (n)
If you supply numbers this works perfectly:
Enter the number 1,2 and 3? 10
Invalid Input, give the number between 1 to 3
Enter the number 1,2 and 3? -1
Invalid Input, give the number between 1 to 3
Enter the number 1,2 and 3? 15
Invalid Input, give the number between 1 to 3
Enter the number 1,2 and 3? 104
Invalid Input, give the number between 1 to 3
But if you supply a single character or a string (type str), you'll get an error:
Enter the number 1,2 and 3? a
ValueError: invalid literal for int() with base 10: 'a'
This is beyond the scope of the question but you might want to look into it.
Anyway, your while condition is setting me off..
It seems that you might be using Python 2 with the print_function imported through __future__. (or else the comparison between different types would raise a TypeError in the while statement).
Check your version of python python -V [in the command line] and:
If using python 2 instead of input() use raw_input():
n = int(raw_input("Enter the number 1, 2, 3: ")
If I am wrong and you are indeed using Python 3.x, use int(input()) as explained.
This question already has answers here:
Asking the user for input until they give a valid response
(22 answers)
Closed 8 years ago.
Sorry I'm a beginner. But like if I had:
x = eval(input('Enter a value for x: '))
How do I make it so that if the person input a string instead of a number it would print "Enter a number" instead of getting an error. Some type of if statement where:
if x == str():
print('Please enter a number')
else:
print('blah blah blah')
It sounds like you are looking for str.isdigit:
>>> x = input(':')
:abcd
>>> x.isdigit() # Returns False because x contains non-digit characters
False
>>> x= input(':')
:123
>>> x.isdigit() # Returns True because x contains only digits
True
>>>
In your code, it would be:
if not x.isdigit():
print('Please enter a number')
else:
print('blah blah blah')
On a side note, you should avoid using eval(input(...)) because it can be used to execute arbitrary expressions. In other words, it is very often a security hole and is therefore considered a bad practice by most Python programmers. Reference:
Is using eval in Python a bad practice?