python程序中无法追踪的错误(使用无限循环查找最大和最小数字)

时间:2018-06-04 12:33:33

标签: python-3.x introduction

我是编程的新手,我编写了这段代码,但在某些方面它出现了故障。因为它没有考虑用于确定最大和最小数量的第一个输入数字。此外,它不会打印错误消息"无效输入"如果在第一次输入时给出了错误的输入。简而言之,它忽略了第一个输入。我无法在逻辑中找到错误。如果您检查我的代码并告诉我逻辑中的错误,那将是一个很大的帮助。

largest = None
smallest = None
num = input('Enter a number: ')
while True:
    num = input('Enter a number: ')
    if num == 'done':    
        break 
    try:
        fnum=int(num)

    except:
        print ('Invalid input')

        continue
    if largest is None:
        largest=fnum
    else:
        if largest<fnum:
            largest=fnum


    if smallest is None:
        smallest=fnum
    else: 
        if smallest>fnum:
            smallest=fnum   

print('Maximum is ',largest)
print('Minimum is ',smallest)

问题1: Here no error message is displayed as invalid input is given in the first input

Problem2: Here '2'(which is first input) is neglected (as it would have been the minimum of all the numbers) and the error message is displayed

Problem3:Here '10'(which is first input) is neglected (as it would have been the maximum of all the numbers) and the error message is displayed

0 个答案:

没有答案