This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 7 years ago.
Could someone please help me to understand why the program doesn't output 'Well done!' after I input 1? Thank you.
while True:
o=input("Enter 1:")
if o==1:
break
print("Well done!")
It looks like you're using python3.x.
On python3.x, input always returns an instance of str, so o will never equal the 1 since you're comparing different types (str and int).
Related
This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 2 years ago.
So I am not great with Python if it is not obvious, but I can't seem to figure out why it is straight up ignoring my if statement. Could anyone please shed some light on this, I have double checked my variable names.
I have more code above this that imports random and does some password checking successfully, but this section of code isn't working as expected.
correct=0
q1a=random.randint(0,10)
q1b=random.randint(0,10)
answer1=q1a+q1b
print(q1a, "+", q1b, "=")
a1=input("Answer: " )
if answer1==a1:
print("Correct!")
correct+=1
print(answer1,a1,correct)```
Typecast input to integer, by default it takes it as a string.
a1 = int(input("Answer:"))
This question already has answers here:
Identifying the data type of an input
(4 answers)
Closed 2 years ago.
I'm unable to compare users input as str or int in following code..
A=input(" ")
if type(A)=str:
Print("this is string")
elif type(A)=int:
Print("this is integer")
So please tell me how to deal with this type of problem
use string class isdigit() method to check user input is number or string.
This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 3 years ago.
I tried writing a code for the collatz func .But somehow i failed in it.i am just sharing the code which i tried.can you figure out the mistake
def my_input():
a=input("enter:")
collatz(a)
def myprint(y):
print(y)
if (y!=1):
my_input()
def collatz(number):
if (number%2)==0:
return myprint(number/2)
else:
return myprint(3*number+1)
my_input()
Your mistake is int(input("enter:")), because you are passing in a string into your function collatz(number), without converting it into int.
Welcome to stackoverflow, next time please include things like your expected outputs or what error you are receiving so that people can easily help you. You can read about how to ask a question here
This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Identifying the data type of an input
(4 answers)
Closed 4 years ago.
I am trying to check if the input is integer. But the code is repetedly saying "something else" in each and every input I give. Is something wrong with the code?
x = input("enter:")
if type(x) == int:
print("int")
else:
print("something else")
if float(x).is_integer():
# do stuff
note, as the comments have said, x here is a string, so you're converting that string to a number first, then checking if it's an integer.
you may also want to wrap this around a try block to catch strings that are not numbers, etc.
This question already has answers here:
What's the difference between `raw_input()` and `input()` in Python 3? [duplicate]
(6 answers)
How can I read inputs as numbers?
(10 answers)
Closed 6 years ago.
I have this line of code:
incoming = input("Type in 1 or 2")
if incoming == 1:
print ("you entered 1")
elif incoming == 2:
print ("you entered 2")
this worked perfectly fine when I used python 2... on my mac, but on windows with python 3, not so well.
Can anybody explain this to me?
Python 3.x doesn't evaluate and convert data types the way Python 2.x did. So, you are going to have to explicitly convert your user's input to an integer like this:
incoming = int(input("Type 1 or 2: "))