【问题标题】:Group if-else statement then execute a random set分组 if-else 语句然后执行一个随机集
【发布时间】:2018-03-20 07:50:58
【问题描述】:

我正在制作一个程序,该程序将生成一个随机琐事测验,每个问题中都包含 if-else 语句和变量。需要知道如何使用 import random 对每个集合进行分组并每次生成一个随机集合,或者如果有其他方法可以建议我。

我的代码:

c1 = 0
c2 = 0
while(1):
  quiz1 = print("What is Prosciutto?")
  q = input().lower()
  if ("italian" in q) or ("dry" in q) or ("ham" in q):
    print("Correct!")
    c1 +=1
  else:
    print("Not quiet right, Prosciutto is Italian dry-cured ham")
    c2 +=1
  input("Press Enter to continue...")
  quiz2 = print("What is the capital of the US state of Alabama?")
  q = input().lower()
  if "montgomery" in q:
    print("Correct!")
    c1 +=1
  else:
    print("Nope, Montgomery, it is.")
    c2 +=1
  input("Press Enter to continue...")
  quiz3 = print("Which planet spins on a nearly horizontal axis?")
  q = input().lower()
  if "uranus" in q:
    print("Correct!")
    c1 +=1
  else:
    print("Actually it is Uranus!")
    c2 +=1
  input("Press Enter to continue...")
  quiz4 = print("Who invented writing?")
  q = input().lower()
  if "sumerian" in q:
    print("Correct!")
    c1 +=1
  else:
    print("Nope, the Sumerians invented writing")
    c2 +=1
  input("Press Enter to continue...")
  quiz5 = print("What rapper was born Marshall Bruce Mathers III?")
  q = input().lower()
  if "eminem" in q:
    print("Correct!")
    c1 +=1
  else:
    print("He's Eminem")
    c2 +=1
  input("Trivia ended, Press Enter to view your result...")
  break
print("You've made", c1, "corrects answers and ", c2, "wrong answers")

【问题讨论】:

    标签: python python-3.x if-statement random python-3.6


    【解决方案1】:

    用 key = question 和 value = answers 创建一个字典,同时列出所有问题。然后导入 random 和 randint 一个介于 0 和 len(question_list) 之间的数字,并向用户显示该问题 - 并检查他的答案是否是字典中的值,其中键是给定问题。就在我的头顶

    【讨论】:

      【解决方案2】:

      首先排除经常出现的“提问并检查答案”模式:

      def handle(question, answer, err):
          print(question)
          a = input().lower()
          if a in answer:
              print("Correct!")
              return True
          else:
              print(err)
              return False
      

      然后定义你的问题/答案/错误:

      QUIZZES = [
          ("What is Prosciutto?", ("italian","cured","dryed","ham"), "Not quiet right, Prosciutto is Italian dry-cured ham"),
          ("What is the capital of the US state of Alabama?", ("montgomery",), "Nope, Montgomery, it is."),
          # etc
          ]
      

      那么你只需要一个 main 函数来运行整个事情:

      def main():
          good = 0
          wrong = 0   
          for question, answers, err in QUIZZES:
              ok = handle(question, answers, err)
              if ok:
                  good += 1
              else:
                  wrong += 1
          input("Trivia ended, Press Enter to view your result...")
          print("You've made {} corrects answers and  {} wrong answers".format(good, wrong))
      

      一旦您到达那里,添加随机化只​​需在 QUIZZES 上调用 random.choice()...

      【讨论】:

      • 我试过了,但它打印了一整套问题,比如 ('美国阿拉巴马州的首府是什么?', 'montgomery', 'Nope, Montgomery, it是。') 而不是一个问题,然后它再次按顺序运行琐事。顺便说一句,我用了import random
      • 在将for question, answers, err in QUIZZES: 替换为while(1): question,answers,err = random.choice(QUIZZES) 后,它现在可以工作了,感谢您的帮助。
      • 您可能希望跟踪已经提出的问题,以避免一遍又一遍地重复相同的测验(提示:使用set 将提出的问题存储在您的 while 循环中)
      • 谢谢 :) 不过,你认为如果它通过少量测验需要很长时间来处理吗?我猜这个集合中的测验很有可能会自行重复,除非我有,比如 100 个测验......
      • 取决于你所说的“需要很长时间来处理”?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-16
      • 2021-06-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多