社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

Python中基于类别的问答题数量

Printer • 4 年前 • 1706 次点击  

我想知道如何根据玩家数量选择添加问题。例如,如果玩家选择有40个问题的测验。这40道题应该包含30%的数学、14%的体育、6%的城市等等。直到他们总结出40个问题。

这个测验也应该加上玩家选择的数量。这些类别只反映测验中有多少类别出现。

这是我的代码:

category = {
    "Math": 30,
    "Sport": 14,
    "Citys": 6,
    "Games": 3,
    "Inventions": 3
}

questions = []
amount = int(input("How many questions would you like? "))
for x in range(amount):
    #if ??
    questions.append(x)
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/128302
文章 [ 2 ]  |  最新文章 4 年前
SAI SANTOSH CHIRAG
Reply   •   1 楼
SAI SANTOSH CHIRAG    5 年前

你可以这样试试。由于没有实际问题的列表,我将附加问题类型:

category = {
    "Math": 30,
    "Sport": 14,
    "Citys": "6",
    "Games": "3",
    "Inventions": 3
}
scale = 0
for i in category:
    scale += i

questions = []
amount = int(input("How many questions would you like? "))

for i in category:
    temp = [i]*(category[i]*amount//scale)
    questions += temp
#print(temp)
Thomas Weller
Reply   •   2 楼
Thomas Weller    5 年前

在开始之前先解决一些问题。

那本字典开头有不同的类型。如果你想计算,使用 int 不 string .它应该被命名 categories 而不是 category 还有,因为他们中有很多人。

categories = {
    "Math": 30,
    "Sport": 14,
    "Citys": 6,
    "Games": 3,
    "Inventions": 3
}

如果这些数字加起来不等于100,我们需要知道它的总数:

ratioTotal = sum(categories.values())

如前所述,询问用户:

questions = []
totalQuestionCount = int(input("How many questions would you like? "))

然后,你需要一个循环来循环所有类别,这样你就可以得到每个类别的问题(注意单数和复数是如何开始有意义的):

for category, relativeAmount in categories.items():

接下来,计算特定类别需要多少问题:

    amountForCategory = round(totalQuestionCount * relativeAmount / ratioTotal)

开始添加问题。因为不需要变量,所以使用 _ 作为discard变量:

    for _ in range(amountForCategory):
        question = GetQuestion(category)
        questions.append(category)

最后,由于舍入误差,您可能会偏离几步,因此您需要进行补偿:

while len(questions) < totalQuestionCount:
    question = GetQuestion("any")
    questions.append(question)

留给你的是:实施实际问题

def GetQuestion(category):
    return "whatever"