【问题标题】:I have random challenges and random answers, but i want the random challenges bound to the right answer我有随机挑战和随机答案,但我希望随机挑战与正确答案绑定
【发布时间】:2019-01-09 23:06:28
【问题描述】:

我正在为我的学校制作一个项目。我需要挑战,如果你按下按钮,你会得到一个随机的挑战,但你也会得到一个随机的答案。我想将挑战限制在正确的答案上。我不知道如何用索引来做,所以如果有人能给我一个例子,我会很感激。

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

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)

预期:您按下生成挑战并获得正确答案。现在的实际结果是你得到一个挑战但随机的答案,所以答案是不正确的。

【问题讨论】:

  • 代码中定义的正确答案在哪里?如果无法通过代码判断正确答案是什么,只有上帝知道正确答案。
  • 是的,这就是我想知道的。我如何将答案与挑战联系起来。结果是“答案”,但并不好。我有 30/40 的挑战,所以也有 30/40 的答案,这就是为什么会有这么多结果,我想要将 2 个结果绑定到 1 个挑战。
  • 您可以简单地将挑战和相应的答案分组到一个列表中。然后将所有这些挑战和答案对分组到另一个列表中。
  • 我的老师也建议过,但我不知道该怎么做。你能不能列一份清单,让我做剩下的,因为我是初学者,不知道如何正确地做。
  • 我只有 6 个问题复制粘贴,否则会太长。但每次 2 个结果“答案”将绑定到 1 个问题。我怎样才能为 1 个问题绑定 2 个结果答案?

标签: python tkinter


【解决方案1】:

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

您的代码也存在一些问题。如果您多次单击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()

结果:

【讨论】:

  • 非常感谢您,先生,这个也为我工作。我很高兴你也帮助了我
【解决方案2】:

这是一个小解决方法,从您现在的位置开始。由于每个问题恰好有 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) 

现在你应该可以开始了!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多