制作简单、中等、困难三个难度级别的测验

发布于 2025-01-18 15:37:22 字数 1807 浏览 0 评论 0原文

所以基本上我必须编写一个程序,询问用户的难度级别并打印问题,甚至是用户提出的问题的限制。我面临一个问题,我无法打印以下问题:

  1. 什么是 21+86?
  2. 什么是92/2?
  3. 什么是86-70?
number_1 = random.randrange(1, 100000)
number_2 = random.randrange(1, 100000)

当我使用这个命令时,我得到像 19899 + 198846 这样的数字。这是我不想要的。我不知道该怎么办。

import random

print ("Welcome Little Brother!")
#This Educational Software will help you with your arithmetic problem and polish your skills.
score = 0
print("Please make a selection from the following:")
print("P: Practice Math.")
print("S: Show Score.")
print("Q: Quit.")

selection = input("What do you want to do:")

if selection == "S" :
    print ("No score found.")
elif selection == "P":
    print("What difficult level do you want:")
    print("E: Easy.")
    print("M: Medium.")
    print("H: Hard.")

level = input("Enter Difficulty Level:")

numberofproblem = int(input("How many problems do you want :"))

max_number = numberofproblem

for question_num in range(1, max_number+1):
    if level == "E" :
        ops = ['+', '-']
    elif level == "M" :
        ops = ['+', '-', '*']
    elif level == "H" :
        ops = ['+', '-', '*','/']
    else:
        print ("Incorrect Menu Key Selected")

    number_1 = random.randrange(1, 100000)
    number_2 = random.randrange(1, 100000)
    operation = random.choice(ops)

    maths = round(eval(str(number_1) + operation + str(number_2)),5)
    print('\nQuestion number: {}'.format(question_num))
    print ("The question is",number_1,operation,number_2)
    answer = float(input("What is your answer: "))
    if answer == maths:
        print("Correct")
        score = score + 1
    else:
        print ("Incorrect. The actual answer is",maths)

So Basically I have to write a program that asks the user for the difficulty level from the user and print questions and even the limit of questions asked from the user. I am facing an issue that I am not able to print questions like these:

  1. What is 21+86?
  2. What is 92/2?
  3. What is 86-70?.
number_1 = random.randrange(1, 100000)
number_2 = random.randrange(1, 100000)

When I use this command I get number like 19899 + 198846. which I don't want. I have no idea what to do.

import random

print ("Welcome Little Brother!")
#This Educational Software will help you with your arithmetic problem and polish your skills.
score = 0
print("Please make a selection from the following:")
print("P: Practice Math.")
print("S: Show Score.")
print("Q: Quit.")

selection = input("What do you want to do:")

if selection == "S" :
    print ("No score found.")
elif selection == "P":
    print("What difficult level do you want:")
    print("E: Easy.")
    print("M: Medium.")
    print("H: Hard.")

level = input("Enter Difficulty Level:")

numberofproblem = int(input("How many problems do you want :"))

max_number = numberofproblem

for question_num in range(1, max_number+1):
    if level == "E" :
        ops = ['+', '-']
    elif level == "M" :
        ops = ['+', '-', '*']
    elif level == "H" :
        ops = ['+', '-', '*','/']
    else:
        print ("Incorrect Menu Key Selected")

    number_1 = random.randrange(1, 100000)
    number_2 = random.randrange(1, 100000)
    operation = random.choice(ops)

    maths = round(eval(str(number_1) + operation + str(number_2)),5)
    print('\nQuestion number: {}'.format(question_num))
    print ("The question is",number_1,operation,number_2)
    answer = float(input("What is your answer: "))
    if answer == maths:
        print("Correct")
        score = score + 1
    else:
        print ("Incorrect. The actual answer is",maths)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

邮友 2025-01-25 15:37:22

根据 documentation randrange()这些值是在范围[a,b)中生成的。

number_1 = random.randrange(1, 100000)
number_2 = random.randrange(1, 100000)

这意味着在您的情况下,生成了1100'000之间的随机值。尝试降低第二个数字,以使生成的任何数字都是可以接受的。 100可能非常适合您的示例。

According to the documentation of randrange(), the values are generated in the range [a,b).

number_1 = random.randrange(1, 100000)
number_2 = random.randrange(1, 100000)

This means that in your case a random value between 1 and 100'000 is generated. Try to lower the second number so that any number generated is acceptable. 100 might be a good fit for your example.

赠我空喜 2025-01-25 15:37:22

您可以制作一个字典来选择难度,并将其分配给一个函数以运行难度测验

from random import choice
def easy():
   CorrectQuestions = 0
   IncorrectQuestions = 0
   #I want 3 easy questions
   easyQuestions = {'Question':'answer','Question':'answer','Question':'answer','Question':'answer'}
   Q1 = choice(easyQuestions)
   Q2 = choice(easyQuestions)
   Q3 = choice(easyQuestions)
   print("Question 1:\n")
   answer = input(Q1)
   if answer == easyQuestions[Q1]:
      print("Correct")
      CorrectAnswers += 1
   else:
      print("Incorrect")
      IncorrectAnswers +=1
   print("Question 2:\n")
   answer2 = input(Q2)
   if answer2 == easyQuestions[Q2]:
      print("Correct")
      CorrectAnswers += 1
   else:
      print("Incorrect")
      IncorrectAnswers += 1
   print("Question 3:\n')
   answer3 = input(Q3)
   if answer3 == easyQuestions[Q3]:
      print("Correct")
      CorrectAnswers += 1
   else:
      print("Incorrect")
      IncorrectAnswers += 1
   results = f"You got {str(CorrectAnswers)} answers correct and {str(IncorrectAnswers)} answers wrong."
def medium():
   #code
def hard():
   #code
diff = {'easy':easy,'medium':medium,'hard':hard}
Difficulty = input("Enter Difficulty:")
for i in diff:
    if(Difficulty == diff[i]:
       diff[i]()
    else:
       continue

You can make a dictionary to choose the difficulty and assign it to a function to run that difficulty quiz

from random import choice
def easy():
   CorrectQuestions = 0
   IncorrectQuestions = 0
   #I want 3 easy questions
   easyQuestions = {'Question':'answer','Question':'answer','Question':'answer','Question':'answer'}
   Q1 = choice(easyQuestions)
   Q2 = choice(easyQuestions)
   Q3 = choice(easyQuestions)
   print("Question 1:\n")
   answer = input(Q1)
   if answer == easyQuestions[Q1]:
      print("Correct")
      CorrectAnswers += 1
   else:
      print("Incorrect")
      IncorrectAnswers +=1
   print("Question 2:\n")
   answer2 = input(Q2)
   if answer2 == easyQuestions[Q2]:
      print("Correct")
      CorrectAnswers += 1
   else:
      print("Incorrect")
      IncorrectAnswers += 1
   print("Question 3:\n')
   answer3 = input(Q3)
   if answer3 == easyQuestions[Q3]:
      print("Correct")
      CorrectAnswers += 1
   else:
      print("Incorrect")
      IncorrectAnswers += 1
   results = f"You got {str(CorrectAnswers)} answers correct and {str(IncorrectAnswers)} answers wrong."
def medium():
   #code
def hard():
   #code
diff = {'easy':easy,'medium':medium,'hard':hard}
Difficulty = input("Enter Difficulty:")
for i in diff:
    if(Difficulty == diff[i]:
       diff[i]()
    else:
       continue
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文