print the result as soon as user input “done”

2019-09-01 15:17发布

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

2条回答
劳资没心,怎么记你
2楼-- · 2019-09-01 15:41

I would do this as follows:

largest_so_far = smallest_so_far = None

while True:
    value = raw_input(">")
    # first deal with 'done'
    if value.lower() == 'done':  
        break
    # then convert to float
    try: 
        value = float(value)
    except ValueError:
        continue  # or break, if you don't want to give them another try
    # finally, do comparisons
    if largest_so_far is None or value > largest_so_far:
        largest_so_far = value
    if smallest_so_far is None or value < smallest_so_far:
        smallest_so_far = value
查看更多
闹够了就滚
3楼-- · 2019-09-01 15:49

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.

查看更多
登录 后发表回答