3

编写一个程序,反复提示用户输入整数,直到用户输入“完成”。输入“完成”后,打印出最大和最小的数字。如果用户输入的不是有效数字,请使用 try/except 捕获它并发出适当的消息并忽略该数字。

到目前为止我有这个但我对如何创建一种比较最大值的方法感到困惑?我是编程新手,我只是寻求帮助。我是否也在尝试之前包含了 try 和 except 块?然后除了错误?

 largest = None
 smallest = None
 while True:
     num = raw_input("Enter a number: ")
     if num == "done" : break
       print num

 print "Maximum", largest
4

8 回答 8

5
nums = []
while True:
  n = raw_input("Enter a number: ")
  if n == "done":
    break
  try:
    nums.append(int(n))
  except ValueError:
    print "Invalid input"

print "Min: %d" % min(nums)
print "Max: %d" % max(nums)
于 2013-01-24T02:46:49.830 回答
3
largest = None
smallest = None
first_number = True

while True:
    num = raw_input("Enter a number: ")
    if num == "done" : break

    try:
        num = int(num)
        if first_number:
            largest = num
            smallest = num
            first_number = False
        else:
            largest = max(largest, num)
            smallest = min(smallest, num)
    except Exception, e:
        print "Not Valid Input!!!"
        continue

print "Maximum", largest
print "Minimum", smallest
于 2013-01-24T02:47:42.580 回答
0

您可以通过对原始程序进行非常小的修改来做到这一点:只需在您考虑它们时保留最小和最大数字的标签。

largest  = None
smallest = None

while True:
    string = raw_input("Enter a number: ")

    if string == "done":
        break

    try:
        num = int(string)
    except ValueError:
        print "Not a number"
        continue

    if largest is None or num > largest:
        largest = num

    if smallest is None or num < smallest:
        smallest = num
于 2014-07-04T05:23:21.787 回答
0
largest =  None
smallest = None

while True:
   num = raw_input("Enter a number: ")
   if num == "done": break
   if len(num) < 1 : break
   try:
    num=int(num)
   except:
    print "Invalid input"
    continue
    if num is smallest:
      smallest = num
    if num > largest:
      largest = num
print "Maximum is ", largest
print "Minimum is ", smallest
于 2014-07-04T05:03:13.387 回答
0
numbers =[]
while True:
     num = raw_input("Enter a number: ")
     if num == "done" :
         break
     else:
         numbers.append(num)

print max(numbers)
print min(numbers)

因此,逻辑是将数字添加到列表中并使用函数 max 和 min。您可以编写代码来自己处理异常。

于 2013-01-24T02:47:29.917 回答
0
largest = None
smallest = None

while True:
num = raw_input('Enter a number: ')

if num == 'done':
    print 'Maximum is %s' % largest
    print 'Minimum is %s' % smallest
    break

try:
    num = int(num)

    if smallest is None or num <= smallest:
        smallest = num

    if largest is None or num >= largest:
        largest = num

except:
    print 'Invalid input'
于 2015-03-09T16:05:15.650 回答
0

我也是一个 Python 初学者,我在 Charles Russell Severance 的“Python for Everyone”中注意到了这个问题。我的答案如下。

prompt = 'Enter the number: '
initial_value = 0.0
while True:
    thing = input(prompt)
    if thing == 'done':
        break
    try:
        num = float(thing)
    except:
        print('Invalid input')
        continue
    num = float(thing)
    if num > initial_value:
        max = num
        min = initial_value
    else:
        min = num
print('Max', max)
print('Min', min)
于 2020-07-13T13:34:30.397 回答
-1

通过分配num给单个值,您将在循环的每次迭代中覆盖它。请改用列表。

num = []
finish = "n"
while finish.lower() == "n"
    try:
        num.append(int(raw_input("Enter a number: ")))
    except ValueError:
         print "Not a number"
    finish = raw_input("Would you like to add another number? (y/n): ")

print max(num)
于 2013-01-24T02:45:52.273 回答