Python for循环列表值

时间:2020-05-10 01:02:39

标签: python

到目前为止,这是我所得到的,我已经尝试了多种方式的for循环,无法将列表值分配给问题,并且如果正确只能添加正确的答案。

.content {
  height:100vh;
 }

1 个答案:

答案 0 :(得分:0)

这是使用for循环组织测试的一种方法

代码

def quest():
  points = [100, 200, 300]

  # Orgainize questions as list of strings
  questions = [
    'What color are apples?\n(a) red\n(b) blue\n(c) yellow\n',
    'What color are blueberries?\n(a) red\n(b) blue\n(c) yellow\n',
    'What color are watermelon?\n(a) green\n(b) blue\n(c) yellow\n']

  # Organize answer as list of letters
  answers = ['a', 'b', 'a']

  score = 0
  for i in range(len(questions)):
    if input(questions[i]) != answers[i]:
      print('Sorry, not correct')
    else:
      score += points[i]

  return score

print(f'Your score is: {quest()}')

简明版本

def quest():
  points = [100, 200, 300]

  # Orgainize questions as list of strings
  questions = [
    'What color are apples?\n(a) red\n(b) blue\n(c) yellow\n',
    'What color are blueberries?\n(a) red\n(b) blue\n(c) yellow\n',
    'What color are watermelon?\n(a) green\n(b) blue\n(c) yellow\n']

  # Organize answer as list of letters
  answers = ['a', 'b', 'a']

  return sum((p for q,a,p in zip(questions, answers, points) if input(q) == a))

print(f'Your score is: {quest()}')
相关问题