2016-06-27 3 views
-1

Как говорится в этом вопросе. Я пытаюсь получить 4 «практических» урока от ученика, сохраняя границы каждого сорта в пределах 0-40. Однако я продолжаю получать сообщение об ошибке с моим кодом:Python: добавление граничных значений в циклы while

while True: 
    try: 
     mark = int(input("What was your first mark out of the four? ")) 
     if mark < 0 or mark > 40 : 
      print("That is outside the score boundaries. Please try again.") 
     else: 
      break 
    except ValueError: 
     print("That isn't a number!") 

counter = 1 
while counter <4: 
    while True: 
     try: 
      mark += int(input("And the next mark? ")) 
      if mark < 0-mark or mark > 40+mark : 
       print("That is outside the score boundaries. Please enter the final three marks again.") 
      else: 
       break 
     except ValueError: 
      print("That isn't a number! Please enter the final three marks again.") 
    counter += 1 

while True: 
    try: 
     end_mark = int(input("What was your end of year mark? ")) 
     if end_mark < 0 or end_mark > 60 : 
      print("That is outside the score boundaries. Please try again.") 
     else: 
      break 
    except ValueError: 
     print("That isn't a number!") 


#calculations 
average = mark/counter 
total_score = average + end_mark 

#processing 
if total_score > 50: 
    if end_mark < 30: 
     print("You pass conditionally with %{}. Your practical mark was averaged at {}, but your end of year mark was only {}.".format(total_score, average, end_mark)) 
    else: 
     print("You pass with %{}. Your practical mark was averaged at {}, and your end of year mark was {}.".format(total_score, average, end_mark)) 
else: 
    print("Im sorry. You failed the year with %{}. Your practical mark averaged at {}, and the end of year mark was {}.".format(total_score, average, end_mark)) 

print("End of Code") 

Как я могу это исправить?

+0

Какая ошибка? – polku

+0

", если знак <0-отметка или знак> 40 + знак:" (второй абзац кода). –

+0

Если я вхожу, например, 54, он будет принимать число, когда im пытается установить границу на 40. –

ответ

0

Это потому, что вы сначала добавляете, а затем проверяете и поэтому mark никогда не будет > 40+mark или < 0-mark.

mark += int(input("And the next mark? ")) 
# The next line is where the problem is 
if mark < 0-mark or mark > 40+mark : # mark will never be > 40+mark or less than 0-mark 
    print("That is outside the score boundaries. Please enter the final three marks again.") 
else: 
    break 

Сделайте это вместо этого.

tmp = int(input("And the next mark? ")) 
    if not (0 <= mark <= 40): 
     print("That is outside the score boundaries. Please enter the final three marks again.") 
    else: 
     mark += tmp 
     break 
+0

Спасибо, что это легче понять, но что означает «tmp»? –

+0

'tmp' - это всего лишь временная переменная для хранения входного значения, пока мы не проверим, что он находится в диапазоне. Отметьте это как ответ, если это поможет вам. –