-3

我试过这样写:

print "Enter numbers, stops when negative value is entered:"
numbers = [input('value: ') for i in range(10)]
while numbers<0:

但突然间,我失去了理智,不知道下一步该怎么办

例子是:

输入数字,输入负值时停止:

价值:5

价值:9

值:2

值:4

价值:8

值:-1

最大值为 9

4

3 回答 3

0

这就是你要找的:

print "Enter numbers, stops when negative value is entered:"
nums = []
while True: # infinite loop
    try: n = int(raw_input("Enter a number: ")) # raw_input returns a string, so convert to an integer
    except ValueError: 
        n = -1
        print "Not a number!"
    if n < 0: break # when n is negative, break out of the loop
    else: nums.append(n)
print "Maximum number: {}".format(max(nums))
于 2013-10-07T17:19:03.393 回答
0

听起来你想要一些类似的东西:

def get_number():
    num = 0
    while True: # Loop until they enter a number
        try:
            # Get a string input from the user and try converting to an int
            num = int(raw_input('value: '))
            break
         except ValueError:
            # They gave us something that's not an integer - catch it and keep trying
            "That's not a number!"

     # Done looping, return our number
     return num

print "Enter numbers, stops when negative value is entered:"
nums = []
num = get_number() # Initial number - they have to enter at least one (for sanity)
while num >= 0: # While we get positive numbers
    # We start with the append so the negative number doesn't end up in the list
    nums.append(num)
    num = get_number()

print "Max is: {}".format(max(nums))
于 2013-10-07T17:20:08.407 回答
0

我想你想要这样的东西:

# Show the startup message
print "Enter numbers, stops when negative value is entered:"

# This is the list of numbers
numbers = []

# Loop continuously
while True:
   
    try:
        # Get the input and make it an integer
        num = int(raw_input("value: "))
    
    # If a ValueError is raised, it means input wasn't a number
    except ValueError:

        # Jump back to the top of the loop
        continue

    # Check if the input is positive
    if num < 0:

        # If we have got here, it means input was bad (negative)
        # So, we break the loop
        break

    # If we have got here, it means input was good
    # So, we append it to the list
    numbers.append(num)

# Show the maximum number in the list
print "Maximum is", max(numbers)

演示:

输入数字,输入负值时停止:

价值:5

价值:9

值:2

值:4

价值:8

值:-1

最大值为 9

于 2013-10-07T17:21:03.557 回答