我有随机挑战和随机答案,但我希望随机挑战绑定到正确答案

时间:2019-01-09 11:29:05

标签: python tkinter

我正在为我的学校做一个项目。我需要挑战,如果您按下按钮,您将获得随机挑战,但您还将获得随机答案。我想将挑战局限于正确的答案。我不知道如何使用索引,所以如果有人可以向我展示一个示例,我将不胜感激。

我已经问过我的老师,搜索了但找不到任何东西。

from tkinter import *
import random

theWindow = Tk()
theWindow.geometry('500x500')
theWindow.title('Challenges')
Label(theWindow, text='Press the button below to generate random challenges', bg= 'grey', fg='white').pack()

challenges = ['You are going through Russia. Do you have the item winterjacket?\n \n Option 1: Yes \n Option 2: No',
          'A thief grabs your bag with items: What are you going to do?\n\n 
Option 1: Not chasing the thief \n Option 2: Chasing the thief',
          'You don’t have money for food anymore:\n You found a job for a week. \n Are you going to take the job?:\n\n Option 1: Yes, take the job \n 
Option 2: No, you don"t take the job',
          'You walk along a grave and hear a sound: \n What are you going to do?: \n \n Option 1: You run away \n Option 2: You take a look',
          'You won an helicopter flight and you’re in an helicopter right now. \n The helicopter starts to fall down. \n What are you going to do?: \n \n Option 1: Grab a parachute and jump out of the helicopter \n Option 2: Stay in the helicopter',
          'You see an old lady carrying an heavy bag. \n What are you going to do?: \n \n Option 1: Walk away \n Option 2: Help the old lady']

Outcome = ['+10 HP','+10 HP','+10 HP','+10 HP','+10 HP','+10 HP','+10 HP','+10 HP and skip 1 turn', '+20 HP','+20 HP',
           '+20 HP','+20 HP', '+30 HP','+30 HP','+30 HP', '+40 HP', '+10 HP + 1 item','+10 HP + 1 item','+10 HP + 1 item',
           '+20 HP + 1 item','+20 HP + 1 item', '+20 HP + 2 item', 'Back to 100 HP', 'Nothing happens',
          '-10 HP','-10 HP','-10 HP','-10 HP', '-20 HP','-20 HP','-20 HP','-20 HP','-20 HP',
          '-20 HP', '-30 HP', 'Lose all HP', '-40 HP', '-50 HP', 'Lose all items', 'Lose all items', 'Skip 1 turn', '-20 HP and skip 1 turn',
          'You have to throw the dice: if you get 1, 3 or 5 you can get an item. If you throw 2, 4 or 6 you will get -10 HP damage.',
          'Nothing happens']

def challenges_button():
    challenge = Label(theWindow, text=random.choice(challenges))
    challenge.place(relx=0.5, rely=0.3, anchor=CENTER)

def answers():
    answer = Label(theWindow, text= random.choice(Outcome))
    answer.place(relx=0.5, rely=0.7, anchor=CENTER)

def answers1():
    answer1 = Label(theWindow, text= random.choice(Outcome))
    answer1.place(relx=0.5, rely=0.7, anchor=CENTER)    

#The buttons
generate_button = Button(theWindow, text='Generate Challenge', height=3, 
width=20, command=challenges_button, bg='black', fg='white')
generate_button.place(relx=0.5, rely=0.1, anchor=CENTER)

button_1_Button = Button(theWindow, text='Option 1', height=1, width=20, 
command=answers, bg='black', fg='white')
button_1_Button.place(relx=0.5, rely=0.55, anchor=CENTER)

button_2_Button = Button(theWindow, text='Option 2', height=1, width=20, 
command=answers1, bg='black', fg='white')
button_2_Button.place(relx=0.5, rely=0.6, anchor=CENTER)

预期:您按生成挑战,您将获得正确的答案。现在的实际结果是您得到了挑战,但答案是随机的,因此答案不正确。

2 个答案:

答案 0 :(得分:2)

您可以做的是,在challenges列表中生成一个随机索引。使用该索引,您可以使用text列表中该索引处出现的挑战来配置标签challenges。与此相对应,您还知道对此的两个答案是2i列表中的第2i+1个元素和第Outcome个元素。

您的代码也有一些问题。如果您多次单击Generate按钮,它们将开始重叠。所以我从功能中删除了它。

import tkinter as tk
import random

theWindow = tk.Tk()
theWindow.title('Challenges')
tk.Label(theWindow, text='Press the button below to generate random challenges', bg= 'grey', fg='white').grid(row=0)

index = None

challenges = ['Q1) You are going through Russia. Do you have the item winterjacket?\n \n Option 1: Yes \n Option 2: No',
          'Q2) A thief grabs your bag with items: What are you going to do?\n\n Option 1: Not chasing the thief \n Option 2: Chasing the thief',
          'Q3) You don’t have money for food anymore:\n You found a job for a week. \n Are you going to take the job?:\n\n Option 1: Yes, take the job \n Option 2: No, you don"t take the job',
          'Q4) You walk along a grave and hear a sound: \n What are you going to do?: \n \n Option 1: You run away \n Option 2: You take a look',
          'Q5) You won an helicopter flight and you’re in an helicopter right now. \n The helicopter starts to fall down. \n What are you going to do?: \n \n Option 1: Grab a parachute and jump out of the helicopter \n Option 2: Stay in the helicopter',
          'Q6) You see an old lady carrying an heavy bag. \n What are you going to do?: \n \n Option 1: Walk away \n Option 2: Help the old lady']

Outcome = ['q1 a1', 'q1 a2', 'q2 a1', 'q2 a2', 'q3 a1', 'q3 a2', 'q4 a1', 'q4 a2', 'q5 a1', 'q5 a2', 'q6 a1', 'q6 a2']

challenge = tk.Label(theWindow)
challenge.grid(row=2)

answer = tk.Label(theWindow)
answer.grid(row=5)

def challenges_button():
    global index
    index = random.choice(range(len(challenges)))
    answer.configure(text="")
    challenge.configure(text=challenges[index])

def answers():
    global index
    answer.configure(text=Outcome[2*index])

def answers1():
    global index
    answer.configure(text=Outcome[2*index+1])

#The buttons
generate_button = tk.Button(theWindow, text='Generate Challenge', height=3, width=20, command=challenges_button, bg='black', fg='white')
generate_button.grid(row=1)

button_1_Button = tk.Button(theWindow, text='Option 1', height=1, width=20, command=answers, bg='black', fg='white')
button_1_Button.grid(row=3)

button_2_Button = tk.Button(theWindow, text='Option 2', height=1, width=20, command=answers1, bg='black', fg='white')
button_2_Button.grid(row=4)

theWindow.mainloop()

结果:

Demo

答案 1 :(得分:1)

这是一个小解决方法,从您现在所在的位置开始。因为每个问题都有2个结果,所以我首先建议将每个挑战的所有“正确答案”分组到子列表中,如下所示(组成这些答案,但您明白了):

new_outcome = [['+10 HP', '-10 HP'], ['+20 HP', '+10 HP'], ...]

还要确保以正确的顺序排列它们,以使new_outcome列表中的第一个答案对属于challenges列表中的第一个挑战,同时也属于选项1的答案。是两个元素中的第一个。


快速编辑:如果我从您对原始问题的评论中得到的答案正确,则您的答案已经按照正确的顺序排列了。因此,只需像这样进行列表理解:

new_outcome = [[Outcome[2*i], Outcome[2*i + 1]] for i in range(int(len(Outcome) / 2))]

创建所需的答案对列表。


现在,您可以创建字典(有关字典的更多信息,请参见here),将挑战用作键,并将每个挑战与相应的答案分组(包装在列表中):

c_and_a = dict()

for item in challenges:
    c_and_a[item] = new_outcome[challenges.index(item)]

>> c_and_a = {'challenge_1':['+10 HP', '-10 HP'], 'challenge_2':['+20 HP', '+10 HP'], ...}

现在,按如下所示定义按钮命令。请注意,我在这里建议的方法要求您在函数外部定义challenge标签,因为所有函数都需要访问它。您可以在底部将其与所有其他小部件一起使用,即使在其上方定义,您的函数仍然可以访问它。

def challenges_button():
    challenge.config(text=random.choice(list(c_and_a.keys())))

c_and_a是字典。在python 3.x中,我假设您是根据tkinter导入来使用的,d.keys()返回字典d的所有键上的迭代器。由于random.choice仅适用于列表,因此我们首先必须通过list()命令将此迭代器转换为列表。

现在,您已经选择了一个挑战,但是用户仍然必须选择一个答案。您可以将随机选择的键存储在变量中,以便稍后使用它来确定正确的答案对,但是我建议您再次阅读challenge标签。最后,在函数外定义一个答案标签,以避免重叠:

def answers():
    chall = challenge.cget('text')  # this will read the challenge label
    answer.config(text=c_and_a[chall][0])  # for option 1

def answers1():
    chall = challenge.cget('text')
    answer.config(text=c_and_a[chall][1])  # for option 2

answer = Label(theWindow)
answer.place(relx=0.5, rely=0.7, anchor=CENTER) 

现在你应该很好!