用户输入“完成"后立即打印结果 [英] print the result as soon as user input "done"

查看:60
本文介绍了用户输入“完成"后立即打印结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

largest_so_far = 无smalest_so_far = 无value = float(raw_input(">"))而值 != 值错误:value = float(raw_input(">"))如果值>迄今为止最大的:maximum_so_far = 值elif 值 ==完成":休息打印最大的_so_far

我认为问题在于 done 是 string 而输入是 float 类型.

我也尝试过使用 value = raw_input(">") 而不是 float(raw_input(">") 来运行它,但结果打印为完成

解决方案

小提示:

  1. 与其立即将用户输入转换为float,不如先检查一下是否done?

  2. 由于您将永远这样做,直到用户输入 done 或出现值错误,请使用无限循环和 try..except,像这样

<小时>

# 开头,负无穷大(最小的数)large_so_far = float("-inf")# 正无穷大(最大的数)minimum_so_far = float("+inf")# 告诉用户如何退出程序打印输入完成退出程序"# 无限循环为真:# 读取用户数据value = raw_input(">")# 检查它是否已经完成,如果它真的完成,则打破它如果值==完成":休息# 尝试将用户输入的值转换为浮点数尝试:值 = 浮点数(值)除了值错误:# 如果我们得到 `ValueError` 打印错误信息并跳到开头打印无效值"继续如果值>迄今为止最大的:maximum_so_far = 值如果值<最小的到目前为止:minimum_so_far = 值打印最大的_so_far打印smallest_so_far

<小时>

编辑后的代码有两个主要问题.

  1. continue 语句应该在 except 块内.否则,总是会跳过比较.

  2. 当你比较两个不同类型的值时,Python 2 不会抱怨它.它只是比较值的类型.因此,在您的情况下,由于您将 largest_so_far 指定为 None,因此将 NoneTypefloat 类型进行比较.

    <预><代码>>>>类型(无)<输入'NoneType'>>>>无 >3.14错误的>>>无 <3.14真的

    因为 float 类型总是小于 None 类型,所以条件

    if value >迄今为止最大的:maximum_so_far = 值

    从未见过.所以你会得到 None.相反,使用 float("- inf") 就像我在我的回答中显示的那样.

largest_so_far = None
smalest_so_far = None

value = float(raw_input(">"))
while value != ValueError:


    value = float(raw_input(">"))
    if value > largest_so_far:      
            largest_so_far = value
    elif value == "done":
        break



print largest_so_far

I think problem with this is that done is string while input is float type.

I have also tried it running using value = raw_input(">") instead of float(raw_input(">") but that prints the result as done

解决方案

Few hints:

  1. Instead of converting the user input to float immediately, why don't you check if it is done first?

  2. Since you are going to do this forever, until user enters done or there is a value error, use an infinite loop and a try..except, like this


# Start with, the negative infinity (the smallest number)
largest_so_far = float("-inf")
# the positive infinity (the biggest number)
smallest_so_far = float("+inf")

# Tell users how they can quit the program
print "Type done to quit the program"

# Infinite loop
while True:

    # Read data from the user
    value = raw_input(">")

    # Check if it is `done` and break out if it actually is
    if value == "done":
        break

    # Try to convert the user entered value to float
    try:
        value = float(value)
    except ValueError:
        # If we got `ValueError` print error message and skip to the beginning
        print "Invalid value"
        continue

    if value > largest_so_far:      
        largest_so_far = value

    if value < smallest_so_far:      
        smallest_so_far = value

print largest_so_far
print smallest_so_far


Edit: The edited code has two main problems.

  1. The continue statement should be within the except block. Otherwise, the comparisons are always skipped at all.

  2. When you compare two values of different types, Python 2, doesn't complain about it. It simply, compares the type of the values. So, in your case, since you assigned largest_so_far as None, the NoneType is compared with the float type.

    >>> type(None)
    <type 'NoneType'>
    >>> None > 3.14
    False
    >>> None < 3.14
    True
    

    Since, the float type is always lesser than None type, the condition

    if value > largest_so_far:      
        largest_so_far = value
    

    is never met. So you will be getting None. Instead, use float("- inf") like I have shown in my answer.

这篇关于用户输入“完成"后立即打印结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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