[英]Python how to only accept numbers as a input
mark= eval(raw_input("What is your mark?"))
try:
int(mark)
except ValueError:
try:
float(mark)
except ValueError:
print "This is not a number"
So I need to make a python program that looks at your mark and gives you varying responses depending on what it is.所以我需要制作一个 python 程序来查看你的标记并根据它是什么给你不同的响应。
However I also need to add a way to stop random text which isn't numbers from being entered into the program.但是,我还需要添加一种方法来阻止将不是数字的随机文本输入到程序中。
I thought I had found a solution to this but it won't make it it past the first statement to the failsafe code that is meant to catch it if it was anything but numbers.我以为我已经找到了一个解决方案,但它不会让它通过第一个语句到故障安全代码,如果它不是数字,它是用来捕获它的。
So pretty much what happens is if I enter hello
instead of a number it fails at the first line and gives me back an error that says exceptions:NameError: name 'happy' is not defined
.所以几乎发生的事情是,如果我输入
hello
而不是数字,它会在第一行失败并返回一个错误,指出exceptions:NameError: name 'happy' is not defined
。
How can I change it so that it can make it to the code that gives them the print statement that they need to enter a number?我怎样才能改变它,以便它可以成为为他们提供他们需要输入数字的打印语句的代码?
remove eval and your code is correct:删除 eval 并且您的代码是正确的:
mark = raw_input("What is your mark?")
try:
int(mark)
except ValueError:
try:
float(mark)
except ValueError:
print("This is not a number")
Just checking for a float will work fine:只需检查浮点数即可正常工作:
try:
float(mark)
except ValueError:
print("This is not a number")
you can use the String object method called isnumeric.您可以使用名为 isnumeric 的 String 对象方法。 it's more efficient than try- except method.
它比 try-except 方法更有效。 see the below code.
看下面的代码。
def getInput(prompt):
value = input(prompt)
while not value.isnumeric():
print("enter a number")
value = input("enter again")
return int(value)
Is it easier to declare a global value than to pass an argument, In my case it's also gives an error.声明一个全局值比传递一个参数更容易吗,在我的情况下它也给出了一个错误。
def getInput():
global value
value = input()
while not value.isnumeric():
print("enter a number")
value = input("enter again")
return int(value)
getInput()
print(value)
#can't comment :) #不能评论:)
You can simply cae to float
or int
and catch the exception (if any).您可以简单地使用
float
或int
并捕获异常(如果有)。 Youre using eval which is considered poor and you add a lot of redundant statements.您正在使用被认为很差的 eval 并且您添加了很多冗余语句。
try:
mark= float(raw_input("What is your mark?"))
except ValueError:
print "This is not a number"
"Why not use eval?" “为什么不使用 eval?” you ask, well... Try this input from the user:
[1 for i in range (100000000)]
你问,好吧...试试来自用户的这个输入:
[1 for i in range (100000000)]
import re
pattern = re.compile("^[0-9][0-9]\*\\.?[0-9]*")
status = re.search(pattern, raw_input("Enter the Mark : "))
if not status:
print "Invalid Input"
Might be a bit too late but to do this you can do this:可能有点太晚了,但要做到这一点,你可以这样做:
from os import system
from time import sleep
while True:
try:
numb = float(input("Enter number>>>"))
break
except ValueError:
system("cls")
print("Error! Numbers only!")
sleep(1)
system("cls")
but to make it within a number range you can do this:但要使其在一个数字范围内,您可以这样做:
from os import system
from time import sleep
while True:
try:
numb = float(input("Enter number within 1-5>>>"))
if numb > 5 or numb < 1:
raise ValueError
else:
break
except ValueError:
system("cls")
print("Error! Numbers only!")
sleep(1)
system("cls")
Actually if you going to use eval() you have to define more things.实际上,如果您要使用eval() ,则必须定义更多内容。
acceptables=[1,2,3,4,5,6,7,8,9,0,"+","*","/","-"]
try:
mark= eval(int(raw_input("What is your mark?")))
except ValueError:
print ("It's not a number!")
if mark not in acceptables:
print ("You cant do anything but arithmetical operations!")
It's a basically control mechanism for eval() .它是eval()的基本控制机制。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.