Python - 用于检查输入的简单程序



我正在为我的代码而苦苦挣扎 - sb 可以提供帮助吗? 为什么当我在 Python 中运行它并输入整数以外的内容时它不打印"无效输入"?

基本上程序应该在无限循环中运行,直到我们输入"完成"。 所以即使在除了它仍然应该提示输入一个数字。

largest = None
smallest = None
while True:
    num = raw_input("Enter a number: ")
    try:
       if num == "done" :
            print "Maximum is", largest
            print "Minimum is", smallest
            exit
       if largest is None:
           largest = num
       elif largest < num:
           largest = num
       if smallest is None:
           smallest = num
       elif smallest > num:
           smallest = num 
    except int(num) == -1:
        print "Invalid input"
        continue
largest = None
smallest = None
try:
    num = raw_input("Enter a number: ")
    x = int(num, base = 10)  
except:
    print "Invalid input"
while num != "done":    
    try:
    num = raw_input("Enter a number: ")
    x = int(num, base = 10)  
    except:
        print "Invalid input"
        continue
    if largest is None:
        largest = num
    elif largest < num:
        largest = num
    if smallest is None:
        smallest = num
    elif smallest > num:
        smallest = num  
print "Maximum is", largest
print "Minimum is", smallest
exit

相关内容

  • 没有找到相关文章

最新更新