Python check if input number is string [duplicate] - python

This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 4 years ago.
When I run the below code, it always returns Number please no matter what I enter. Why does this happen?
number = input("please type number: ")
if type(number) != int:
print('Number please')
else:
print('number')

Try this, this should work:
string = input()
if string.isalpha():
print("It is a string. You are correct!")
elif string.isdigit():
print("Please enter a string.")
else:
print("Please enter a proper string.")
The .isalpha is used for checking if it's a string. The .isdigit is used for checking if it's a number.
Hope it helps :)

Related

How to convert a string to integer entered in input functon? [duplicate]

This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 2 years ago.
number=input('Enter a number? ')
print(number)
I want to convert this in single statement.
Just you can use int():
number=int(input('Enter a number? '))
print(number)
Otherwise, to be fool-proof, use try..except:
while True:
try:
number=int(input("Enter a number ? "))
break
except ValueError:
print("Please input a vaild number !!")

Exception if user enters a string instead of an integer [duplicate]

This question already has answers here:
Asking the user for input until they give a valid response
(22 answers)
Closed 2 years ago.
I am trying to make my program print(invalid input, try again) if the user types a string instead of an integer.
num_dice = 0
while True:
if num_dice < 3 or num_dice > 5 or num_dice:
num_dice = int(input("Enter the number of dice you would like to play with 3-5 : "))
print("")
print("Please enter a value between 3 and 5.")
print("")
continue
else:
break
You can simply use the isnumeric keyword to check if it is an absolute number or not.
Example:
string1="123"
string2="hd124*"
string3="helloworld"
if string1.isnumeric() is True:
#Proceed with your case.
inputs=string1
Documentation reference : https://www.w3schools.com/python/ref_string_isnumeric.asp
P.S. this will require you changing your input to string format, as isnumeric validates only string.
This below part I mean.
num_dice = str(input("Enter the number of dice you would like to play with 3-5 : "))

I am forming an if-else code in python and the output should be Correct Answer , but It's printing the else part of the code, I don't know why [duplicate]

This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 4 years ago.
The code:
user_answer = input("What is 8+13=")
answer = 21
if user_answer == answer:
print("Correct answer!")
else:
print("Try Again!")
Even if it takes input as 21, it prints the else part of the code
The input function returns the entered text as a string, see the API documentation:
If the prompt argument is present, it is written to standard output without a trailing newline. The function then reads a line from input, converts it to a string (stripping a trailing newline), and returns that.
Then your code is comparing the string "21" to an integer value 21, comparing a string with an int will evaluate to false.
You could convert the string to an integer value (you can use int):
user_answer = input("What is 8+13")
answer=21
if int(user_answer) == answer:
print("Correct answer!")
else:
print("Try again!")
but this will raise an error if the input can't be converted to an int value. Alternatively convert the int to a string:
user_answer = input("What is 8+13")
answer=21
if user_answer == str(answer):
print("Correct answer!")
else:
print("Try again!")

How do I check if a input is an integer type [duplicate]

This question already has answers here:
How do I determine if the user input is odd or even?
(4 answers)
Closed 4 years ago.
I tried to make a odd/even 'calculator' in python and it keeps popping up errors. Here's the code:
def odd_even():
print("Welcome to Odd/Even")
num = input("Pick a number: ")
num2 = num/2
if num2 == int:
print("This number is even")
else:
print("This number is odd")
Id like to know whats causing the errors and solutions to them
There is an error in the line: num = input("Pick a number: ")
Because input method always returns a String,so you should convert it into int to performs the integer operation
The currect code is:
num =int( input("Pick a number: "))
you can't do math with strings convert it to int
try:
num = int(input("Pick a number: "))
except ValueError:
print('This is not a number!')
return

testing for number or NOT integer in if/while/for [duplicate]

This question already has answers here:
Asking the user for input until they give a valid response
(22 answers)
Closed 4 years ago.
I am trying to get a user to input a number into a calculator program. The goal right now is to keep giving the user an error message and asking to reenter a number until they enter a real number.
here is some code that I've done:
number_1 = input("Enter your first number here! ")
while number_1 == int():
print("Well done for entering a number!")
you need to use isdigit()
number_1 = input("Enter your first number here! ")
if str(number_1).isdigit():
print("Well done for entering a number!")
How about try: except:?
def my_input(number_1):
while 1:
try:
int(number_1)
return number_1
except:
print("It is not an integer.")
number_1 = input("Give an integer: ")
int1 = input("Enter your first number here! ")
my_input(int1)
print("Well done for entering a number!")
You may have to change int(number_1) to float(number_1) (and input text(s)) depending on your implementation.

Categories