如何要求一个数字或字母

问题描述:

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退出,因为它是一个int q是不能接受的,因为它是一个STR时获得一个有效的答案。如何要求一个数字或字母

我试过问单独的问题,但它不能正确计数循环。然后我试着ord ('q')这是113。我不知道如何让这个成功申请,或者如果有更好的方法。

取而代之的是价值直接转换为int的,应先检查其内容:

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

value = int(value) 
+0

太谢谢你了。这非常有帮助 – user2175776 2013-03-15 22:57:47