如何使变量等同于另一个变量(Python)

时间:2015-11-27 17:56:14

标签: python variables random printing equals

我甚至不知道如何解释这个。

Question1 = "a" 
Question2 = "b" 
Question3 = "c" 
Question4 = "d"
Question5 = "e"

Answer1 = "a" 
Answer2 = "b" 
Answer3 = "c" 
Answer4 = "d" 
Answer5 = "e"

等。

questioninteger = random.randint(1,20)
if(questioninteger == 1):
    Boolean1 = True
    Question == Question1
    Answer == Answer1
    FlashCard()
if(questioninteger == 2):
    Boolean2 = True
    Question == Question2
    Answer == Answer2
    FlashCard()
if(questioninteger == 3):
    Boolean3 = True
    Question == Question3
    Answer == Answer3
    FlashCard()

print("")
print(Question)
print("")
key = raw_input()
if(key == Answer):
    print("Correct!")
    time.sleep(1)
    QuestionPicker()

(所有都在函数内)

问题是Python不会改变变量Question,也不会出现错误。答案已成功更改,问题'只是赢了。

1 个答案:

答案 0 :(得分:0)

全局变量通常是一个坏主意;你最好将你的代码重写为

from random import choice
from time import sleep

class Question:
    def __init__(self, question, options, answer):
        self.question = question
        self.options  = options
        self.answer   = answer

    def ask(self):
        print("\n" + self.question)
        for opt in self.options:
            print(opt)
        response = input().strip()
        return (response == self.answer)

# define a list of questions
questions = [
    Question("What is 10 * 2?",                 ["5", "10", "12", "20", "100"],            "20"),
    Question("Which continent has alligators?", ["Africa", "South America", "Antarctica"], "South America")
]

# ask a randomly chosen question
def ask_a_question(questions):
    q      = choice(questions)
    got_it = q.ask()
    sleep(1)
    if got_it:
        print("Correct!")
    else:
        print("Sorry, the proper answer is " + q.answer)