-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

私はあなたがこのようなものが欲しいと思います:

# 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 に答える
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 に答える