【发布时间】: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 个结果答案?