如何使用while语句确保一维数组有效?

时间:2018-10-10 16:24:44

标签: python arrays loops while-loop

我正在尝试检查每个整数(数组)是否有效(介于0和30之间)。当告诉用户分数无效的行运行时出现问题,但是该变量似乎不是False,而且我不知道为什么,有人可以解决此问题吗?

这是有问题的代码:

while valid_score == True and program_running == True:
      for counter in range(0,6):
            print("How mant points did player", counter + 1 ,"earn?") 
            score_earned[counter] = int(input())

            if score_earned[counter] < 0 or score_earned[counter] > 30: 
                  print("That value was invalid as it was lower than 0 or `above 30!")`
                  valid_score = False

            else:
                  valid_score = True


            total_score = score_earned[counter] + total_score

      valid_score = False

4 个答案:

答案 0 :(得分:1)

在将这些值传递到字典之前,您可以阻止输入的任何points都不在您期望的范围内。 您可以使用while循环来完成此操作,该循环仅接受points的范围

score_earned = {}  
players = 5

for i in range(1, players +1):
    points = -1
    while points < 0 or points > 30:
        try:
            points = int(input('Enter points for player {} between 0 and 30: '.format(i)))
        except ValueError:
            print('Please enter points between 0 and 30')
    score_earned[i] = points

total_score = sum(score_earned.values())
print('The total score is: {}'.format(total_score))

答案 1 :(得分:0)

我的猜测是您误解了循环的工作原理

while valid_score == True and program_running == True: # <-- This will not break while running inner loop
      for counter in range(0,6): # <-- this loops independently
            ....

我的建议是调整您的代码,使其看起来像这样:

  for counter in range(0,6):
        print("How mant points did player", counter + 1 ,"earn?") 
        score_earned[counter] = int(input())

        if score_earned[counter] < 0 or score_earned[counter] > 30: 
              print("That value was invalid as it was lower than 0 or `above 30!")`
              valid_score = False
              break # <-- this ends the loop early

        else:
              valid_score = True

        total_score = score_earned[counter] + total_score
        if not program_running == True:
              break

答案 2 :(得分:0)

我继续制作了您的代码的有效版本。您似乎犯了几个错误。这应该有助于清除您的误解。考虑添加type()检查,以防有人决定输入字符串。

score_earned = {} #python dictionary is an associative array
counter = 1
valid_score = True

while valid_score == True: #and program_running == True: #program_running variable is never assigned
    print("How mant points did player", counter, "earn?") 
    score_earned[counter] = int(input())

    if score_earned[counter] < 0 or score_earned[counter] > 30: 
        print("That value was invalid as it was lower than 0 or above 30!")
        valid_score = False

    total_score = score_earned[counter] + total_score #currently doing nothing with total_score
    counter += 1

答案 3 :(得分:0)

for循环的目的有些令人困惑,因为当您可能只想仅使用while循环并在小于6的计数器值递增时,便会在0和6之间反复循环。

我写了这个示例,将我从代码逻辑中可以理解的内容转换为一个while语句:

counter = 0
valid_score = True
program_running = True

while valid_score and counter < 6 and program_running:
    print("How mant points did player", counter + 1 ,"earn?") 
    score_earned[counter] = int(input())

    if score_earned[counter] < 0 or score_earned[counter] > 30: 
        print("That value was invalid as it was lower than 0 or `above 30!")`
        valid_score = False

    total_score += score_earned[counter]  # Not sure you want to add to the total score when invalid
                                          # Probably better to add to total score in else statement
    counter += 1  # Increment counter variable keeping track of number of iterations

您可能可以对其进行一些修改以符合您的预期结果,但这应该可以帮助您了解如何使用while循环并更好地进行计数。