如果主题不等于那么它就没有被定义

时间:2018-05-19 13:22:42

标签: python

对不起标题,因为我不确定它应该是什么。我得到了这个任务,我必须从列表中选择两个主题并添加价格,我的代码是:

If Not IsDBNull(Me.OrgcustomerBindingSource.Current("parentID")) And Me.OrgcustomerBindingSource1.Count = 0 Then
    Me.OrgcustomerTableAdapter1.Fill(Me.LookupCustomer.orgcustomer)

    Dim dr As DataRow = DirectCast(Me.OrgcustomerBindingSource1.AddNew, DataRowView).Row
    dr.BeginEdit()
    'Add every column her with ID of 0?
    dr.Item("ID") = 0
    dr.Item("CompanyName") = ""
    '(...)

    Me.Validate()
    Me.OrgcustomerBindingSource1.EndEdit()
    Me.OrgcustomerTableAdapter1.Update(Me.LookupCustomer.orgcustomer)

    'Me.ParentIDComboBox.Items.Insert(0, String.Empty)
End If

当我输入英语和科学输出" math"未定义

3 个答案:

答案 0 :(得分:3)

让事情变得更整洁:

a = input("Choose two subjects: ")
a = a.split()

# define the prices here
prices = {'english': 100, 'science': 125, 'stem': 200, 'math': 100}

# using list comprehension. What his means:
# for all courses in the input, return me their value from the prices-dict
# and then sum them up
cost = sum(prices[course.lower()] for course in a)

print(cost)

答案 1 :(得分:1)

首先必须通过指定一些值来定义,例如,您可以这样做:

eng = 0
sci = 0
math = 0
stem = 0

然后是你的其余代码:

print("choose two subjects")
print("English $100")
print("Math $75")
print("Science $125")
print("Stem $200")
a = input("Choose two subjects: ")
a = a.split()
for x in a:
  x = x.lower()
  print(x)
  if x == "english":
    eng = 100
  elif x is not "english":
    eng = int(0)

  elif x == "science":
    sci = 125
  elif x is not "science":
    sci = int(0)

  elif x == "stem":
    stem = 200
  elif x is not "stem":
    stem = int(0)

  elif x == "math":
    math = 100
  elif x is not "math":
    math = int(0)
print(eng + sci + math + stem)

答案 2 :(得分:0)

您没有定义某些变量,因此无法访问它们。

使用字典表示可变数量的变量

这是一种可以简化逻辑的方法:

a = input("Choose two subjects: ")
# english science

a = a.split()

mapper = {'english': 100, 'science': 125, 'stem': 200, 'math': 100}
d = {}

for x in a:
    x = x.lower()
    d[x] = mapper[x]

print(d)
# {'english': 100, 'science': 125}

print(sum(d.values()))
# 225