添加输入检查器后如何解决输入不显示的问题

时间:2019-05-13 05:09:59

标签: python

没有输入检查器,我的代码可以在用户输入数字时检查偶数/奇数的数量。但是,按照我的教授的要求,我需要添加一个输入检查器,我不确定为什么这样做,但是它破坏了代码,并且仅在输入非整数时才有效。在清理代码方面的任何帮助也将不胜感激。

def odd_even():
  while True:
    try:
      result = int(input("enter in a number to find odd and even numbers: "))
    except ValueError:
       print("Not a number! Try again.")
       continue
    else:
       return result
       break
  array1 = []
  even = 0
  for i in range(result):
    array1.append(i+1)
    if array1[i] % 2 == 0:
       even = even + 1
    else:
        odd = even + 1
  print ("there are",odd,"odd numbers")
  print ("there are",even,"even numbers")
odd_even()

4 个答案:

答案 0 :(得分:0)

您只是在中断时return正在查看结果。另外,continue是多余的,因为循环中没有代码可执行。

def odd_even():
  while True:
    try:
      result = int(input("enter in a number to find odd and even numbers: "))
    except ValueError:
       print("Not a number! Try again.")
    else:
       break

对于其余的代码,您可以使用除法而不用循环:

def odd_even():
  while True:
    try:
      result = int(input("enter in a number to find odd and even numbers: "))
    except ValueError:
       print("Not a number! Try again.")
    else:
       break
  q, r = divmod(result, 2) # the same as q, r = result // 2, result % 2
  print("There are", q, "even numbers")
  print("There are", q + r, "odd numbers")

示例输出:

>>> odd_even()
enter in a number to find odd and even numbers: a
Not a number! Try again.
enter in a number to find odd and even numbers: b
Not a number! Try again.
enter in a number to find odd and even numbers: c
Not a number! Try again.
enter in a number to find odd and even numbers: 10
There are 5 even numbers
There are 5 odd numbers

>>> odd_even()
enter in a number to find odd and even numbers: f
Not a number! Try again.
enter in a number to find odd and even numbers: g
Not a number! Try again.
enter in a number to find odd and even numbers: h
Not a number! Try again.
enter in a number to find odd and even numbers: 15
There are 7 even numbers
There are 8 odd numbers

答案 1 :(得分:0)

我认为您是错误地添加了一个return语句,这弄乱了流程,请尝试以下操作。

def odd_even():
    while True:
        try:
            result = int(input("enter in a number to find odd and even numbers: "))
        except ValueError:
            print("Not a number! Try again.")
            continue
        else:
            break
    array1 = []
    even = 0
    for i in range(result):
        array1.append(i+1)
        if array1[i] % 2 == 0:
            even = even + 1
        else:
            odd = even + 1
    print ("there are",odd,"odd numbers")
    print ("there are",even,"even numbers")

odd_even()

答案 2 :(得分:0)

我认为您必须将所有工作代码放入try块中。我也为您做了一些清理工作。

def odd_even():
    try:
        even = 0
        odd = 0

        result = int(input("\nEnter in a number to find odd and even numbers: "))

        for i in range(1, result + 1):
            if i % 2 == 0:
                even += 1

            else:
                odd += 1

        print("There are", odd, "odd numbers")
        print("There are", even, "even numbers")

    except ValueError:
        print("Not a number! Try again.")
        odd_even()


while True:
    odd_even()

答案 3 :(得分:0)

我只删除了返回结果就解决了它,并删除了不需要的继续。

def odd_even():
  while True:
    try:
      result = int(input("enter in a number to find odd and even numbers: "))
    except ValueError:
       print("Not an integer! Try again.")
    else:
       break 
  array1 = []
  even = 0
  for i in range(result):
    array1.append(i+1)
    if array1[i] % 2 == 0:
       even = even + 1
    else:
        odd = even + 1
  print ("there are",odd,"odd numbers")
  print ("there are",even,"even numbers")
odd_even()