1
def main ():
    input('press enter to begin testing...')

    counter = 0
    total = 0;

    value = int( input ("Enter temp:  or q to quit " ))

    maxval = value
    minval = value

    while value:
        counter += 1
        total += value

        if value > maxval:
            maxval = value
        if value < minval:
            minval = value

        value = int( input ("Enter temp:  or q to quit "))

    print ( 'Total is:  ', total)
    print ('Counter is: ', counter)

    if counter != 0:
        print ('Average is: ', total / counter)
        print ('Minimum is:  ', minval)
        print ('Maximum is:   ', maxval)

问题是当要求一个tempq退出时,因为它是一个 intq不被接受,因为它是一个 str。

我尝试提出单独的问题,但它没有正确计算循环。然后我尝试ord ('q')了哪个是113. 我不确定如何使它成功应用,或者是否有更好的方法。

4

1 回答 1

1

与其将值直接转换为int,不如先检查其内容:

value = input ("Enter temp:  or q to quit ")
if value == 'q':
    return

value = int(value)
于 2013-03-15T22:49:49.940 回答