我可以在if else语句中命名变量吗?蟒蛇

时间:2017-03-07 15:00:34

标签: python

在这种情况下,我对python和任何语言都很陌生。一周前我开始学习。我正在尝试创建这个程序,只需将你的字母成绩计算出来就可以为你计算GPA。直到我收到错误的结尾都是顺利的。我会告诉你错误

这是代码,错误信息将显示在底部。

$def grades(grades_combined):
  print "This program will tell you what your total GPA for the semester is."
  print "Just enter your grades, and Voila!"

a_1 = 4.4
a = 4.0
a_2 = 3.6
b_1 = 3.4
b = 3.0
b_2 = 2.6
c_1 = 2.4
c = 2
c_2 = 1.6
d_1 = 1.4
d = 1
d_2 = 0.6
f = 0

print """We are going to ask you for all letter grades, in any order. Please
put the letter first, and then the symbol (if any). If there are non-applicable
grades (such as those not entered), just click return."""
print "Press return to continue"
raw_input("Continue!")

grade_1 = raw_input("Grade #1: ")
grade_2 = raw_input("Grade #2: ")
grade_3 = raw_input("Grade #3: ")
grade_4 = raw_input("Grade #4: ")
grade_5 = raw_input("Grade #5: ")
grade_6 = raw_input("Grade #6: ")
grade_7 = raw_input("Grade #7: ")
grade_8 = raw_input("Grade #8: ")


if grade_1 == "a+":
  grade_1_no = a_1
elif grade_1 == "a":
  grade_1_no = a
elif grade_1 == "a-":
  grade_1_no = a_2
elif grade_1 == "b+":
  grade_1_no = b_1
elif grade_1 == "b":
  grade_1_no = b
elif grade_1 == "b-":
  grade_1_no = b2
elif grade_1 == "c+":
  grade_1_no = c_1
elif grade_1 == "c":
  grade_1_no = c
elif grade_1 == "c-":
  grade_1_no = c_2
elif grade_1 == "d+":
  grade_1_no = d_1
elif grade_1 == "d":
  grade_1_no = d
elif grade_1 == "d-":
  grade_1_no = d_2
elif grade_1 == "f":
  grade_1_no = f

  if grade_8 == "a+":
    grade_8_no = a_1
  elif grade_8 == "a":
    grade_8_no = a
  elif grade_8 == "a-":
    grade_8_no = a_2
  elif grade_8 == "b+":
    grade_8_no = b_1
  elif grade_8 == "b":
    grade_8_no = b
  elif grade_8 == "b-":
    grade_8_no = b_2
  elif grade_8 == "c+":
    grade_8_no = c_1
  elif grade_8 == "c":
    grade_8_no = c
  elif grade_8 == "c-":
    grade_8_no = c_2
  elif grade_8 == "d+":
    grade_8_no = d_1
  elif grade_8 == "d":
    grade_8_no = d
  elif grade_8 == "d-":
    grade_8_no = d_2
  elif grade_8 == "f":
    grade_8_no = f

    if grade_2 == "a+":
      grade_2_no = a_1
    elif grade_2 == "a":
      grade_2_no = a
    elif grade_2 == "a-":
      grade_2_no = a_2
    elif grade_2 == "b+":
      grade_2_no = b_1
    elif grade_2 == "b":
      grade_2_no = b
    elif grade_2 == "b-":
      grade_2_no = b_2
    elif grade_2 == "c+":
      grade_2_no = c_1
    elif grade_2 == "c":
      grade_2_no = c
    elif grade_2 == "c-":
      grade_2_no = c_2
    elif grade_2 == "d+":
      grade_2_no = d_1
    elif grade_2 == "d":
      grade_2_no = d
    elif grade_2 == "d-":
      grade_2_no = d_2
    elif grade_2 == "f":
      grade_2_no = f

      if grade_3 == "a+":
        grade_3_no = a1
      elif grade_3 == "a":
        grade_3_no = a
      elif grade_3 == "a-":
        grade_3_no = a_2
      elif grade_3 == "b+":
        grade_3_no = b_1
      elif grade_3 == "b":
        grade_3_no = b
      elif grade_3 == "b-":
        grade_3_no = b_2
      elif grade_3 == "c+":
        grade_3_no = c_1
      elif grade_3 == "c":
        grade_3_no = c
      elif grade_3 == "c-":
        grade_3_no = c_2
      elif grade_3 == "d+":
        grade_3_no = d_1
      elif grade_3 == "d":
        grade_3_no = d
      elif grade_3 == "d-":
        grade_3_no = d_2
      elif grade_3 == "f":
        grade_3_no = f

        if grade_4 == "a+":
          grade_4_no = a_1
        elif grade_4 == "a":
          grade_4_no = a
        elif grade_4 == "a-":
          grade_4_no = a_2
        elif grade_4 == "b+":
          grade_4_no = b_1
        elif grade_4 == "b":
          grade_4_no = b
        elif grade_4 == "b-":
          grade_4_no = b_2
        elif grade_4 == "c+":
          grade_4_no = c_1
        elif grade_4 == "c":
          grade_4_no = c
        elif grade_4 == "c-":
          grade_4_no = c_2
        elif grade_4 == "d+":
          grade_4_no = d_1
        elif grade_4 == "d":
          grade_4_no = d
        elif grade_4 == "d-":
          grade_4_no = d_2
        elif grade_4 == "f":
          grade_4_no = f

          if grade_5 == "a+":
            grade_5_no = a_1
          elif grade_5 == "a":
            grade_5_no = a
          elif grade_5 == "a-":
            grade_5_no = a_2
          elif grade_5 == "b+":
            grade_5_no = b_1
          elif grade_5 == "b":
            grade_5_no = b
          elif grade_5 == "b-":
            grade_5_no = b_2
          elif grade_5 == "c+":
            grade_5_no = c_1
          elif grade_5 == "c":
            grade_5_no = c
          elif grade_5 == "c-":
            grade_5_no = c2
          elif grade_5 == "d+":
            grade_5_no = d1
          elif grade_5 == "d":
            grade_5_no = d
          elif grade_5 == "d-":
            grade_5_no = d2
          elif grade_5 == "f":
            grade_5_no = f

            if grade_6 == "a+":
              grade_6_no = a_1
            elif grade_6 == "a":
              grade_6_no = a
            elif grade_6 == "a-":
              grade_6_no = a_2
            elif grade_6 == "b+":
              grade_6_no = b_1
            elif grade_6 == "b":
              grade_6_no = b
            elif grade_6 == "b-":
              grade_6_no = b_2
            elif grade_6 == "c+":
              grade_6_no = c_1
            elif grade_6 == "c":
              grade_6_no = c
            elif grade_6 == "c-":
              grade_6_no = c_2
            elif grade_6 == "d+":
              grade_6_no = d_1
            elif grade_6 == "d":
              grade_6_no = d
            elif grade_6 == "d-":
              grade_6_no = d_2
            elif grade_6 == "f":
              grade_6_no = f

              if grade_7 == "a+":
                grade_7_no = a_1
              elif grade_7 == "a":
                grade_7_no = a
              elif grade_7 == "a-":
                grade_7_no = a_2
              elif grade_7 == "b+":
                grade_7_no = b_1
              elif grade_7 == "b":
                grade_7_no = b
              elif grade_7 == "b-":
                grade_7_no = b_2
              elif grade_7 == "c+":
                grade_7_no = c_1
              elif grade_7 == "c":
                grade_7_no = c
              elif grade_7 == "c-":
                grade_7_no = c_2
              elif grade_7 == "d+":
                grade_7_no = d_1
              elif grade_7 == "d":
                grade_7_no = d
              elif grade_7 == "d-":
                grade_7_no = d_2
              elif grade_7 == "f":
                grade_7_no = f


grades_combined = ((grade_1_no + grade_2_no + grade_3_no + grade_4_no + grade_5_no,
+ grade_6_no + grade_7_no + grade_8_no)/8)
print "Your total GPA is %r" % grades_combined $

这是错误

Traceback (most recent call last): File "STUDY19", line 252, in <module> grades_combined = ((grade_1_no + grade_2_no + grade_3_no + grade_4_no + grade_5_no, NameError: name 'grade_2_no' is not defined $

请大家帮忙!

4 个答案:

答案 0 :(得分:3)

试试这个。阅读代码,尝试了解我正在做的事情。阅读Python中的列表和dicts。关于理解。关于循环。

#!/usr/bin/env python

def grade_set(c):
    i = ord('d') - ord(c) + 1.0
    return [ (c + '+', i+0.4), (c, i), (c + '-', i-0.4) ]

GRADES_NUMBERS = dict(sum([ grade_set(c) for c in 'abcd' ], []))
GRADES_NUMBERS['f'] = 0.0

grades = [ None ] * 8
for i in range(len(grades)):
  while True:  # until grade is valid
    grades[i] = raw_input("Grade #%d: " % (i+1))
    if grades[i] in GRADES_NUMBERS:
      break
    print "This is no valid grade, please repeat."

grades_numbers = [ GRADES_NUMBERS[grade] for grade in grades ]

grades_combined = sum(grades_numbers) / len(grades_numbers)

print "Your total GPA is", grades_combined

计算机的主要思想是让计算机完成重复性任务,而不是程序员。所以一旦你在编程时重复自己,你就会做错事。这在编程界已经深深扎根,他们甚至还有一个首字母缩略词:DRY - Don不要重复自己。

答案 1 :(得分:0)

从我所看到的,您所要做的就是在启动程序之前创建变量名称。 因此,在定义程序之前,只需初始化变量。

grade_no_1 = ''
grade_no_2 = ''
>>>
grade_no_8 = ''

答案 2 :(得分:0)

这不起作用的原因是因为您的变量不存在于else范围之外。看一下这个例子

x = 1
z = 0
if x < 0:
  y = 2
else:
  z = x + y  # this will fail because y does not exist in a place that can be accessed

如果您这样做,它将起作用:

x = 1
y = 0
z = 0
if x < 0:
  y = 2
else:
  z = x + y  

此外,我建议您查看dictionariesmaps,以大大提高代码的可读性。

答案 3 :(得分:0)

非常感谢你,摆脱了我的问题,但创造了另一个问题。我不知道这个错误意味着什么,我认为它与字符串中的正符号有关。我很抱歉这些愚蠢的问题,正如我刚才说的那样,我只有14岁。

Traceback (most recent call last): File "STUDY19.py", line 261, in <module> + grade_6_no + grade_7_no + grade_8_no)/8) TypeError: bad operand type for unary +: 'str'