如何检查用户输入是否是浮动的 [英] How to check if a user input is a float

查看:152
本文介绍了如何检查用户输入是否是浮动的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是原始代码,我们被要求改变它,以便它可以接受其中没有0和1的数字。



def gold_room():
print这个房间充满了黄金。 ?
$ b $ next = raw_input(>)

如果下一个为0或下一个为1:
how_much = int(next)

else:
dead(Man,learn to type a number。)

if how_much< 50:
print不错,你不贪心,你赢了!
exit(0)

else:
dead(You greedy bastard!)

这是我的解决方案,运行良好,可以识别浮点值:

  def gold_room():
print这个房间里充满了黄金,你拿的百分比是多少?
$ b $ next = raw_input(>)

尝试:
how_much = float(next)
除ValueError:
print人,学会打一个号码。
gold_room()

如果how_much <= 50:
print很好,你不贪心,你赢了!
exit(0)

else:
dead(You greedy bastard!)

通过类似的问题搜索,我找到了一些答案,帮助我编写了另一个解决方案,如下面的代码所示。问题是,使用isdigit()不允许用户输入一个浮点值。所以如果用户说要拿50.5%,就会告诉他们学习如何输入一个数字。它适用于整数。


$ b

def gold_room():
printThis this this get this around?房间充满了黄金,你拿的是多少?

next = raw_input(>)

而真:
if next.isdigit():
how_much = float(next)

如果how_much <= 50:
print很好,你不贪心,你赢了!
exit(0)

else:
dead(You greedy bastard!)

else:
print键入一个数字。
gold_room()


解决方案

<$ c $如果 next 已经从一个字符串转换过来,那么c> isinstance(next,(float,int))这不是在这种情况下。因此,如果您想避免使用 try..except 。,则必须使用 re / p>

我建议使用前面的 try..except 块来代替 if..else block,但是把更多的代码放在里面,如下所示:

  def gold_room():
,True:
print这个房间里充满了黄金,你拿的是多少?
try:
how_much = float(raw_input(>))

如果how_much< = 50:
print不错,你不贪心, 你赢了!
exit(0)

else:
dead(You greedy bastard!)

除了ValueError:
print学会键入一个数字。

这将尝试将其转换为浮点数,如果失败将会引发 ValueError 将被捕获。要了解更多信息,请参阅上面的 Python教程。 p>

I'm doing Learn Python the Hard Way exercise 35. Below is the original code, and we're asked to change it so it can accept numbers that don't have just 0 and 1 in them.

def gold_room():
    print "This room is full of gold. How much do you take?"

    next = raw_input("> ")

    if "0" in next or "1" in next:
        how_much = int(next)

    else:
        dead("Man, learn to type a number.")

    if how_much < 50:
        print "Nice, you're not greedy, you win!"
        exit(0)

    else:
        dead("You greedy bastard!")

This is my solution, which runs fine and recognizes float values:

def gold_room():
    print "This room is full of gold. What percent of it do you take?"

    next = raw_input("> ")

    try:
        how_much = float(next)
    except ValueError:
        print "Man, learn to type a number."
        gold_room()

    if how_much <= 50:
        print "Nice, you're not greedy, you win!"
        exit(0)

    else:
        dead("You greedy bastard!")

Searching through similar questions, I found some answers that helped me write another solution, shown in the below code. The problem is, using isdigit() doesn't let the user put in a float value. So if the user said they want to take 50.5%, it would tell them to learn how to type a number. It works otherwise for integers. How can I get around this?

def gold_room():
    print "This room is full of gold. What percent of it do you take?"

    next = raw_input("> ")

while True:
    if next.isdigit():
        how_much = float(next)

        if how_much <= 50:
            print "Nice, you're not greedy, you win!"
            exit(0)

        else:
            dead("You greedy bastard!")

    else: 
        print "Man, learn to type a number."
        gold_room()

解决方案

isinstance(next, (float, int)) will do the trick simply if next is already converted from a string. It isn't in this case. As such you would have to use re to do the conversion if you want to avoid using try..except.

I would recommend using the try..except block that you had before instead of a if..else block, but putting more of the code inside, as shown below.

def gold_room():
    while True:
        print "This room is full of gold. What percent of it do you take?"
        try:
            how_much = float(raw_input("> "))

            if how_much <= 50:
                print "Nice, you're not greedy, you win!"
                exit(0)

            else:
                dead("You greedy bastard!")

        except ValueError: 
            print "Man, learn to type a number."

This will try to cast it as a float and if it fails will raise a ValueError that will be caught. To learn more, see the Python Tutorial on it.

这篇关于如何检查用户输入是否是浮动的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆