2015-11-27 69 views
-1

我甚至不知道如何解释这一个。如何使变量等于另一个变量(Python)

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不会更改变量问题,并且不会出现错误。 '答案已成功更改,'问题'不会。

+3

你有平等检查(如'问题== Question3'),而不是任务。更改为分配并重试。 – Netch

+1

它也看起来像你会受益于查找如何使用['dicts'](https://docs.python.org/2/library/stdtypes.html#mapping-types-dict) –

+0

感谢耶稣为你,有用。原来我早些时候尝试过,但没有全球化问题。再次感谢 – oisinvg2001

回答

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) 
相关问题