Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如果主题不等于,则不定义它_Python - Fatal编程技术网

Python 如果主题不等于,则不定义它

Python 如果主题不等于,则不定义它,python,Python,对不起,我不知道标题应该是什么。我被赋予这个任务,我必须从列表中选择两个主题,并将价格加起来,我的代码是: 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

对不起,我不知道标题应该是什么。我被赋予这个任务,我必须从列表中选择两个主题,并将价格加起来,我的代码是:

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)

当我输入英语和科学时出错,输出的数学没有定义

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

使用字典查找可变数量的变量

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

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

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

使用字典查找可变数量的变量

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

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

您必须首先通过分配一些值来定义,例如,您可以执行以下操作:

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)

您必须首先通过分配一些值来定义,例如,您可以执行以下操作:

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)

让事情变得更整洁:

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)

让事情变得更整洁:

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)


你能提供你正在键入的输入吗?在for循环外部,将所有值设置为0数学,英语等选择两个主题:科学和英语。你能提供你正在键入的输入吗?在for循环外部,将所有值设置为0数学,英语,选择两个主题:scinece和English。我将详细说明这一点,用elif x==数学,elif x不是数学,这两个if语句的计算结果都不是真的。这意味着永远不会创建数学,因此给出的数学不是定义的错误。我不明白为什么这会被升级。变量的初始声明确实是个问题,但还有很多其他错误:使用is进行字符串比较,使用elif而不是if,这样就永远不会达到某种情况。例如,stem和math将返回0。我将详细说明这一点,用elif x==math和elif x不是math,这两个if语句的计算结果都不是true。这意味着永远不会创建数学,因此给出的数学不是定义的错误。我不明白为什么这会被升级。变量的初始声明确实是个问题,但还有很多其他错误:使用is进行字符串比较,使用elif而不是if,这样就永远不会达到某种情况。例如,stem和math将返回0。在这种方法中,您损失的是对最终结果有贡献的分数和科目分数组合。当然,这是一个小问题,但值得一提。如果你能把你所做的事情做成一个图表,那有可能吗?@jpp-True。我在问题中没有将其视为一项要求,因此我将重点放在了结果上。@DevPatel你所说的图表是什么意思?我的意思是,如果你能给我看一下你的代码流程图,是否可能。在这种方法中,你失去的是最终结果的分数和科目分数组合。当然,这是一个小问题,但值得一提。如果你能把你所做的事情做成一个图表,那有可能吗?@jpp-True。我在问题中没有将其视为一项要求,因此我将重点放在结果上。@DevPatel你所说的图表是什么意思?我的意思是,如果你能给我看一下你的代码流程图,可以吗。