如何使一个变量等同于另一个变量(Python)
我甚至不知道怎么解释这个如何使一个变量等同于另一个变量(Python),python,variables,random,printing,equals,Python,Variables,Random,Printing,Equals,我甚至不知道怎么解释这个 Question1 = "a" Question2 = "b" Question3 = "c" Question4 = "d" Question5 = "e" 等等 等等 等等 (全部在功能范围内) 问题是Python不会更改变量问题,并且不会出现错误。”答案已成功更改,“问题”将不会更改。全局变量通常是个坏主意;您最好将代码重写为 from random import choice from time import sleep class Question:
Question1 = "a"
Question2 = "b"
Question3 = "c"
Question4 = "d"
Question5 = "e"
等等
等等
等等
(全部在功能范围内)
问题是Python不会更改变量问题,并且不会出现错误。”答案已成功更改,“问题”将不会更改。全局变量通常是个坏主意;您最好将代码重写为
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)
您有相等检查(如
Question==Question3
)而不是作业。更改为分配,然后重试。它看起来也会让您受益于如何使用感谢耶稣,它很有效。事实证明我早些时候就试过了,但并没有把这个问题全球化。再次感谢
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()
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)