📜  python 要求真实值,直到负值显示最高和最低 - Python 代码示例

📅  最后修改于: 2022-03-11 14:45:31.056000             🧑  作者: Mango

代码示例1
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))