且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何检查用户输入是否是浮动的

更新时间:2023-11-28 08:23:16

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