This question already has answers here:
How do I check if a string represents a number (float or int)?
(39 answers)
Closed 7 years ago.
How can I make this work?
number = int(raw_input("Number: "))
if number != !!!!!!!!NUMBER TYPE IS NOT AN INT TYPEEE!!!!!!!!!
print "NO!"
else
print "YES!"
Thanks!! :)
try:
num = int(raw_input("Number: "))
print("Yes!")
except ValueError:
print("No!")
number = raw_input("Number: ")
if not number.isdigit():
print "NO!"
else:
print "YES!"
Related
This question already has answers here:
Asking the user for input until they give a valid response
(22 answers)
Closed 4 months ago.
num = int(input("Number: "))
if num != int:
def summer():
summed = 0
for i in str(num):
summed += int(i)
print(summed)
if len(str(num)) != 5:
print("Type a number with 5 letters")
else:
print(*str(num), sep=", ")
summer()
You should use try
while True:
try:
num = int(input('Number : '))
break
except ValueError:
print('Value is not an integer, repeat')
continue
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 !!")
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 :)
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
This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 7 years ago.
numbers = [1,2,3,4,5,6,7]
x = input()
if x in numbers:
print("Hey you did it")
else:
print("Nope")
I'm not sure what I'm doing wrong here, but it always tells me that my number is not in the list.. even though it is. Works fine with strings, though.
Help would be appreciated. Thanks!
Input is a string, so you are comparing strings to integers. First convert to an int then do the membership test:
numbers = [1,2,3,4,5,6,7]
x = input()
if int(x) in numbers:
print("Hey you did it")
else:
print("Nope")
To make this slightly more robust, you should handle the ValueError that will occur if the user does not input an integer (there's always one user who will enter 'cheeseburger' instead of a number):
numbers = [1,2,3,4,5,6,7]
x = input()
try:
i = int(x)
if i in numbers:
print("Hey you did it")
else:
print("Nope")
except ValueError:
print("You did not enter a number")