Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何为浮点捕获各种类型的错误输入?_Python - Fatal编程技术网

Python 如何为浮点捕获各种类型的错误输入?

Python 如何为浮点捕获各种类型的错误输入?,python,Python,我是一名明天有评估的在校学生,我的练习代码如下: '''A local technology company has various options for buying cell phones on credit: • Pay off monthly over six month with no interest • Pay off monthly over 12 months with 5% interest on the money owing after 6 months and

我是一名明天有评估的在校学生,我的练习代码如下:

'''A local technology company has various options for buying cell phones on credit:
•   Pay off monthly over six month with no interest
•   Pay off monthly over 12 months with 5% interest on the money owing after 6 months and up to 12 months

They have asked you to design and create a program that they can use to work out the various values depending on the customer’s requirements and initial sales price. The initial sales price includes GST. As output, they want the name of the person displayed, sales price, interest to be paid, the term of repayment and the amount they will be paying each month.

The program needs to run continuously until a rogue value Q is entered. You are expected to design and implement a user-friendly approach. Also make sure the program is robust enough to survive any random values entered.  Use test data that will cover all possible combinations.
Created By: William
Date: 26th June 2016'''

#Get users name and trap false inputs
name = input("What is your name? ")
while name.isnumeric() or name == "":
    print("That isn't a valid option. Please enter an alphabetical name.")
    name = input("What is your name? ")

#get sales price and trap false inputs
sales_price = float(input("How much does your phone cost? $"))
while sales_price.isnumeric() or sales_price == "":
    print("That isn't a number! Please enter a value above $0")
    sales_price = float(input("How much does your phone cost? $"))

print(name, sales_price)
我的问题是,如描述中所述,如何在销售价格中捕获字母或负值等输入?
非常感谢您的帮助。

您应该利用以下事实:
float()
在收到无法转换为float的字符串时引发
ValueError

在下面的代码中,只要用户输入负数或无法转换为浮点的字符串,就会重复
while
循环

valid_input = False
while not valid_input:
    try:
        sales_price = float(input("How much does your phone cost? $"))
        if sales_price < 0:
            raise ValueError
    except ValueError:
        pass
    else:
        valid_input = True
valid\u输入=False
虽然不是有效的\u输入:
尝试:
销售价格=浮动(输入(“您的手机多少钱?$”)
如果销售价格<0:
升值误差
除值错误外:
通过
其他:
有效输入=真

您应该利用这样一个事实,即
float()
在给定无法转换为float的字符串时会引发
ValueError

在下面的代码中,只要用户输入负数或无法转换为浮点的字符串,就会重复
while
循环

valid_input = False
while not valid_input:
    try:
        sales_price = float(input("How much does your phone cost? $"))
        if sales_price < 0:
            raise ValueError
    except ValueError:
        pass
    else:
        valid_input = True
valid\u输入=False
虽然不是有效的\u输入:
尝试:
销售价格=浮动(输入(“您的手机多少钱?$”)
如果销售价格<0:
升值误差
除值错误外:
通过
其他:
有效输入=真

我建议使用
试一下,除了像这样的块:

correct_number = False
while correct_number == False:
    try:
        x = abs(float(input('>> Enter float number  ')))
        correct_number = True
    except ValueError:
        print('not correct input')

print(x)       

我建议使用
try,但类似这样的块除外:

correct_number = False
while correct_number == False:
    try:
        x = abs(float(input('>> Enter float number  ')))
        correct_number = True
    except ValueError:
        print('not correct input')

print(x)       

非常感谢。另外,我是将“valid_input”存储为变量还是..?@WillMurton如第一行所示,
valid_input
是一个布尔值。很抱歉,我听起来很傻。我以前没有使用过try和except值。那么,“valid\u input=False”对while语句有什么影响呢?
valid\u input
被设置为
False
while
循环的条件为
while not valid\u input
。换言之,“只要
有效输入
”。
valid\u input
不为
False
,即
True
,唯一的方法是
float()
成功地将输入转换为float,并且输入为正数。谢谢!另外,我是将“valid_input”存储为变量还是..?@WillMurton如第一行所示,
valid_input
是一个布尔值。很抱歉,我听起来很傻。我以前没有使用过try和except值。那么,“valid\u input=False”对while语句有什么影响呢?
valid\u input
被设置为
False
while
循环的条件为
while not valid\u input
。换言之,“只要
有效输入
”。
valid\u input
不为
False
,即
True
,唯一的方法是
float()
都成功地将输入转换为float,并且输入是正数。这与用户意外输入负数类似,但也会处理问题-它会被转换为正数。但一旦你们确定输入可以被正确地解释为浮点数,你们就可以对它进行任何额外的检查(符号、模等)。它类似于但也可以处理你们的问题,若用户不小心输入负数,它会被转换成正数。但一旦您确定输入可以被正确地解释为float,您就可以对其执行任何附加检查(符号、模等)