如何让我的代码一次读取一个以上的数字?一位数输入有效,但两位数或更多输入无效

How to make my code read more than one digit at a time? One digit input works but two-digit or more inputs does not work

我是 Python 的初学者。我的代码适用于一位数,但不适用于两位数。我知道 for 循环一次读取一位数字的用户输入,但我不知道如何让它像在数组中一样读取我的输入。前任。 5,7,9 读作 5,7,9 但 5,70, 9 读作 5, 7, 9.

largest = None
smallest = None
while True:
    try:
        num = input("Enter a number: ")
        num1 = float(num)

    except:
        if "done" == num:
            break
        print("Invalid input")
        continue

    for value in num:
        if smallest is None:
            smallest = num
        if num < smallest:
            smallest = num
            #print(smallest, num)
        elif largest is None:
            largest = num
        if num > largest:
            largest = num
            #print("Maximumcc is:", largest)

print("Maximum is:", largest)
print("Minimum is: ", smallest)

你循环错了。

您需要在逗号字符处拆分输入。然后遍历这些元素,将它们中的每一个转换为 float 并测试它。

largest = None
smallest = None
while True:
    response = input("Enter numbers separated by comma, or 'done': ")
    if response == 'done':
        break

    try:
        nums = [float(num) for num in response.split(',')]
    except:
        print("Invalid input")
        continue

    for num in nums:
        if smallest is None or num < smallest:
            smallest = num
        if largest is None or num > largest:
            largest = num

print("Maximum is:", largest)
print("Minimum is: ", smallest)