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 !!")
Related
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 : "))
This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 4 years ago.
am trying to take input from user forcefully and execute it in Fibonacci elements program, having problem near input i want to make sure that he doesn't enter character nor -ve number, when i take my input as int(input()) my while loop wont execute i want it to keep executing until user provides input.
in below program when i entered 5 as input it taking it as string object
n=input("enter no of fibonnaci elements: ")
while not n:
n=input("enter no of fibonnac elements: ")
print(type(n))
if(n!=int()):
print("enter integer only")
else:
t1=0
t2=1
print("series is:",end=" ")
for i in range(n):
print(t1,end=" ")
t1,t2=t2,(t1+t2)
print()
You can use isinstance(n,int)to check if it is an int. But if you use int(input("enter no of fibonnaci elements:"))then it will throw a ValueError before it.
n=int(input("enter no of fibonnaci elements: ")) #convert to int here
while not n: #will not enter this loop - why is it even here?
n=input("enter no of fibonnac elements: ")
if(not isinstance(n,int)): #Note: won't Reach this line - Will throw an error before this
print("enter integer only")
else:
t1=0
t2=1
print("series is:",end=" ")
for i in range(n):
print(t1,end=" ")
t1,t2=t2,(t1+t2)
print()
A better way to do this
while True:
try:
n=int(input("enter no of fibonnaci elements: "))#convert to int here
break
except ValueError:
print("enter integer only")
t1=0
t2=1
print("series is:",end=" ")
for i in range(n):
print(t1,end=" ")
t1,t2=t2,(t1+t2)
print()
Output
enter no of fibonnaci elements: str
enter integer only
enter no of fibonnaci elements: 6
series is: 0 1 1 2 3 5
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 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!"