Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/300.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 如何仅使用random.randint()将列表随机化?(闭门)_Python - Fatal编程技术网

Python 如何仅使用random.randint()将列表随机化?(闭门)

Python 如何仅使用random.randint()将列表随机化?(闭门),python,Python,我目前正在尝试获取一个问题和答案列表(QandA),并在每次迭代中随机地列出这些问题。我知道我可以使用choice(),但我只学过random.randint()中的随机化。有人能帮我吗?在当前状态下,会提出问题和答案,但不是随机的。元组和其他更高级的方法不能使用,因为在我的课程中,我只能使用所教的内容 我尝试使用random.randint(0,len(QandA)-1)认为这会使列表随机化,但并没有达到预期的结果 import random print("Welcome to Te Reo

我目前正在尝试获取一个问题和答案列表(QandA),并在每次迭代中随机地列出这些问题。我知道我可以使用choice(),但我只学过random.randint()中的随机化。有人能帮我吗?在当前状态下,会提出问题和答案,但不是随机的。元组和其他更高级的方法不能使用,因为在我的课程中,我只能使用所教的内容

我尝试使用random.randint(0,len(QandA)-1)认为这会使列表随机化,但并没有达到预期的结果

import random
print("Welcome to Te Reo Maori Quiz!!!")
print("\nAnswer the questions with single Maori words")
QandA = ['A challenge laid down in chant and dance:','haka',
             'What is "Sun" in Maori:', 'ra',
             'Something you eat to fill your belly:', 'kai',
             'Type in the Maori word for "cave":', 'ana',
             'Traditional Maori food cooked in an earth oven:','hangi',
             'Ma is white, whero is red, kakariki is green, pango is black. What else is black?:', 'mangu',
             'its getting ... in here, so take off all your clothes:', 'wera',
             'What does Kia ora mean?:', 'hello',
             'What does ka pai mean?:', 'good',
             'What does kei te peha koe mean?:', 'how are you',
             'What is the Maori phrase for "what is your name?:', 'ko wai to ingoa',
             'What does hikoi mean?:', 'walk',
             'What is a waiata:', 'song',
             'What is the the Maori word for stomach?:', 'puku',
             'What does mahi mean?', 'work',
             'What is the maori word for wait?:', 'taihoa',
             'if something was nui, then it would be what?:', 'big',
             'What does Haere mai mean? (hint: it starts with "w"):', 'welcome',
             'What does nau mai mean?:', 'welcome',
             'What does tangi mean?:', 'funeral',
             ]

points = 0
current = 0
quiz = 0

while(quiz < 5):

    current = random.randint(0, len(QandA) - 1)

    question = input("Q" + str(quiz + 1) + ". " + QandA[current])
    newquestion = question.upper()
    if question == QandA[current + 1]:
        points = points + 10
        current = current + 2
        print("\nCorrect Answer!")

    elif question == newquestion:
        print("\nInvalid! Please enter only lowercase characters!")
        current = current + 2

    else:
        print("\nIncorrect answer. The correct answer is:", QandA[current+1])
        points = points - 10
        current = current + 2
    quiz = quiz + 1

    if points < 0:
        points = 0

print("End of Quiz.")
print("Your score: %", points, sep = "") 
随机导入
打印(“欢迎来到Te Reo毛利问答!!!”)
打印(\n用一个毛利语单词回答问题)
QandA=[“在吟唱和舞蹈中提出的挑战:”,“哈卡”,
毛利语中的“太阳”是什么,
“你用来填饱肚子的东西:”、“凯”,
'输入毛利语中“洞穴”的单词:''ana',
“传统毛利食物在土烤箱中烹调:”、“汉吉”,
“马是白色的,whero是红色的,kakariki是绿色的,pango是黑色的。还有什么是黑色的?”,
“快…进来了,脱下你所有的衣服:”,“wera”,
“Kia ora是什么意思?”:“你好”,
“卡派是什么意思?”:“好”,
“kei te peha koe是什么意思?”:“你好吗?”,
“你叫什么名字?”的毛利语是什么,
“hikoi是什么意思?”:“走”,
“什么是Waita:”,“song”,
毛利语中胃的意思是什么?:,“puku”,
“mahi是什么意思?”,“工作”,
“等待”的毛利词是什么?:,“太和”,
“如果某个东西是nui,那么它会是什么?”:“大”,
“Haere mai是什么意思?(提示:它以“w”开头):”,“欢迎”,
“nau mai是什么意思?”:“欢迎”,
“tangi是什么意思?”:“葬礼”,
]
分数=0
电流=0
测验=0
而(测验<5):
电流=random.randint(0,len(QandA)-1)
问题=输入(“Q”+str(测验+1)+“+QandA[当前])
newquestion=question.upper()
如果问题==QandA[当前+1]:
点数=点数+10
电流=电流+2
打印(“\n正确答案!”)
elif问题==新问题:
打印(“\n无效!请只输入小写字符!”)
电流=电流+2
其他:
打印(“\n正确答案。正确答案为:”,QandA[current+1])
点数=点数-10
电流=电流+2
测验=测验+1
如果点<0:
分数=0
打印(“测验结束”)
打印(“您的分数:%”,分数,sep=”“)

预期的结果是在每次迭代时让列表随机化,并获得随机问题的正确答案。

我认为使用
random.randint(0,len(QandA)-1)没有问题
获取随机索引。您遇到的问题来自您的数据结构。将问题和答案作为不同的元素将更难保证您的索引是一个问题

也许您可以将数组更改为元组数组:

QandA = [('A challenge laid down in chant and dance:','haka'),
             ('What is "Sun" in Maori:', 'ra'),
             ('Something you eat to fill your belly:', 'kai'),
             ('Type in the Maori word for "cave":', 'ana'),
             ('Traditional Maori food cooked in an earth oven:','hangi'),
             ('Ma is white, whero is red, kakariki is green, pango is black. What else is black?:', 'mangu'),
             ('its getting ... in here, so take off all your clothes:', 'wera'),
             ('What does Kia ora mean?:', 'hello'),
             ('What does ka pai mean?:', 'good'),
             ('What does kei te peha koe mean?:', 'how are you'),
             ('What is the Maori phrase for "what is your name?:', 'ko wai to ingoa'),
             ('What does hikoi mean?:', 'walk'),
             ('What is a waiata:', 'song'),
             ('What is the the Maori word for stomach?:', 'puku'),
             ('What does mahi mean?', 'work)',
             ('What is the maori word for wait?:', 'taihoa'),
             ('if something was nui, then it would be what?:', 'big'),
             ('What does Haere mai mean? (hint: it starts with "w"):', 'welcome'),
             ('What does nau mai mean?:', 'welcome'),
             ('What does tangi mean?:', 'funeral'),
             ]
现在您可以执行以下操作:

current = random.randint(0, len(QandA) - 1)
然后通过以下方式访问:

q_a = QandA[current]
question = q_a[0]
answer = q_a[1]
编辑: 如果不能使用元组,另一种选择是使用两个单独的列表,并使用相同索引的Q&A

current = random.randint(0, len(questions) - 1)
question = questions[current]
answer = answers[current]

这是一个只需要
random.randint()
的解决方案

从列表中弹出一个随机项并附加到新列表中。这样,您就不会有重复项

请注意,原始列表会在过程中被删除,但如果您将来需要它,您可以随时复制它

import random

x = ['A','B','C','D','E','F']
rnd_x = []
while len(x):
    try:
        rnd_x.append(x.pop(random.randint(0,len(x))))
    except IndexError:
        pass

print(rnd_x)
#['F', 'B', 'C', 'D', 'A', 'E']

提示,想想索引。从第一眼看,对
QandA
进行简单的随机化将改变问题与其答案相邻的方式。这是一种预期行为?对,我只是尝试对问题进行随机化。使用单独的列表对此有帮助吗?制作元组怎么样:
[(Q1,A1),…,(Qn,an)]
,并将元组列表随机排列,这样您就可以始终得到问题的相应答案?我不能使用元组,因为我没有被教过。在我的课程中,我只能使用所教的内容。在这一点上,我的知识非常基础。如果OP没有提到
元组
不是一个选项,这将是最好的答案。也许可以调整回答一点以便它使用(可能)两个列表?是的,很遗憾,元组不是一个选项。如果我有两个列表,我将如何编写random.randint代码?谢谢!这就是为什么我在发布后编辑了它。只需声明两个列表q=[Q1,Q2,Q3..Qn]和a=[A1,A2,A3…an]。然后使用任何列表获取随机索引(它们的大小应该相同)您是否检查过两个列表的元素数是否相同?