Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python编程语言_Python_Python 2.7 - Fatal编程技术网

Python编程语言

Python编程语言,python,python-2.7,Python,Python 2.7,我被困在这个问题上,它在Python中: 编写一个程序,反复提示用户输入整数,直到用户输入“完成”。输入“完成”后,打印出最大和最小的数字。如果用户输入的不是有效号码,请用try/except捕捉它,并发出相应的消息,忽略该号码。 这是我现在拥有的代码: largest = None smallest = None while True: num = raw_input("Enter a number: ") if num == "done" : break tr

我被困在这个问题上,它在Python中:

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

这是我现在拥有的代码:

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

     try:
          num = float(num)
     except:
          print "Invalid input"
          continue 

     if num > largest:
         largest = num
     elif num < smallest:
         smallest = num

     largest = str(largest)
     smallest = str(smallest)    


print "Maximum " + "is " + largest
print "Minimum " + "is " + smallest
max=None
最小=无
尽管如此:
num=原始输入(“输入一个数字:”)
如果num==“完成”:中断
尝试:
num=浮点(num)
除:
打印“无效输入”
持续
如果num>最大值:
最大=num
elif num<最小值:
最小=num
最大=str(最大)
最小=str(最小)
打印“最大值”+“是”+最大值
打印“最小值”+“是”+最小值

您需要删除这两行

 largest = str(largest)
 smallest = str(smallest)    
因此,在循环退出之前不会执行它们

或者干脆不使用它们,让
print
进行转换

print "Maximum is ", largest
print "Minimum is ", smallest
如果循环中的值
None

    if num is None or num > largest:
         largest = num
    if smallest is None or num < smallest:
         smallest = num
如果num为None或num>最大:
最大=num
如果最小值为无或num<最小值:
最小=num

您有一些缩进问题,逻辑不起作用。如果您以不同的顺序输入数字,它将输出错误的结果。我在代码中添加了注释供您查看

largest = None
smallest = None

while True:
    # Move try here, as it would previously crash if you left a blank line
    try:
        num = raw_input("Enter a number: \n")
    except:
        # Field was left blank
        print "Invalid input"
        continue

    if num == "done":
        break
    else:
        if num.lstrip('-').replace('.', '').isdigit(): # No need for a try here, check if it's a valid number (replace and lstrip to include floats and negatives)
            num = float(num)
            # Auto-set if it's None
            if largest is None or num > largest:
                largest = num
            if smallest is None or num < smallest:
                smallest = num
        else:
            # Field was not a number
            print "Invalid input"

# Print results
print "Maximum is {0}".format(largest)
print "Minimum is {0}".format(smallest)
max=None
最小=无
尽管如此:
#移动并在此处尝试,因为如果您留下一个空行,它以前会崩溃
尝试:
num=原始输入(“输入一个数字:\n”)
除:
#字段留空
打印“无效输入”
持续
如果num==“完成”:
打破
其他:
如果num.lstrip('-').replace('.','').isdigit():#不需要在此处尝试,请检查它是否为有效数字(replace和lstrip包括浮点数和负数)
num=浮点(num)
#如果没有,则自动设置
如果最大值为无或num>最大值:
最大=num
如果最小值为无或num<最小值:
最小=num
其他:
#字段不是一个数字
打印“无效输入”
#打印结果
打印“最大值为{0}”。格式(最大值)
打印“最小值为{0}”。格式(最小值)

这里没有问题。我觉得不错。我不明白,这是一个问题吗?你没有问问题,也没有说明你的代码有问题。阅读帮助页面。