所以我正在做一个关于Python的测验作为编程入门课程的项目。
我的测验按预期工作,除非测验变量不受空白数组的新值影响。在run_quiz函数中,我想通过在用户提供之后将空白更改为正确的答案来使测验变量自行更新。
这是我的代码:
#Declaration of variables
blank = ["___1___", "___2___", "___3___", "___4___"]
answers = []
tries = 5
difficulty = ""
quiz = ""
#Level 1: Easy
quiz1 = "Python is intended to be a highly " + blank[0] + " language. It is designed to have an uncluttered " + blank[1] + " layout, often using English " + blank[2] + " where other languages use " + blank[3] + ".\n"
#Level 2: Medium
quiz2 = "Python interpreters are available for many " + blank[0] + " allowing Python code to run on a wide variety of systems. " + blank[1] + " the reference implementation of Python, is " + blank[2] + " software and has a community-based development model, as do nearly all of its variant implementations. " + blank[1] + " is managed by the non-profit " + blank[3] + ".\n"
#Level 3: Hard
quiz3 = "Python features a " + blank[0] + " system and automatic " + blank[1] + " and supports multiple " + blank[2] + " including object-oriented, imperative, functional programming, and " + blank[3] + " styles. It has a large and comprehensive standard library.\n"
#Answer and quiz assignment
def assign():
global difficulty
global quiz
x = 0
while x == 0:
user_input = raw_input("Select a difficulty, Press 1 for Easy, 2 for Medium or 3 for Hard.\n")
if user_input == "1":
answers.extend(["readable", "visual", "keywords", "punctuation"])
difficulty = "Easy"
quiz = quiz1
x = 1
elif user_input == "2":
answers.extend(["operating systems", "cpython", "open source", "python software foundation"])
difficulty = "Medium"
quiz = quiz2
x = 1
elif user_input == "3":
answers.extend(["dynamic type", "memory management", "programming paradigms", "procedural"])
difficulty = "Hard"
quiz = quiz3
x = 1
else:
print "Error: You must select 1, 2 or 3.\n"
x = 0
def run_quiz():
n = 0
global tries
global blank
print "Welcome to the Python Quiz! This quiz follows a fill in the blank structure. You will have 5 tries to replace the 4 blanks on the difficulty you select. Let's begin!\n"
assign()
print "You have slected " + difficulty + ".\n"
print "Read the paragraph carefully and prepare to provide your answers.\n"
while n < 4 and tries > 0:
print quiz
user_input = raw_input("What is your answer for " + blank[n] + "? Remember, you have " + str(tries) + " tries left.\n")
if user_input.lower() == answers[n]:
print "That is correct!\n"
blank[n] = answers[n]
n += 1
else:
print "That is the wrong answer. Try again!\n"
tries -= 1
if n == 4 or tries == 0:
if n == 4:
print "Congratulations! You are an expert on Python!"
else:
print "You have no more tries left! You can always come back and play again!"
run_quiz()
我知道我的代码有很多改进的地方,但这是我的第一个Python项目,所以我猜这是预期的。
答案 0 :(得分:1)
问题是你的变量quiz
只是一个固定的字符串,虽然看起来它与blanks
有关,但它实际上并没有。你想要的是'字符串插值'。 Python允许使用.format
str
个.format
方法。这实际上是你问题的症结所在,使用字符串插值很容易。我建议你花一些时间来学习quizzes = [
("""\
Python is intended to be a highly {} language.\
It is designed to have an uncluttered {} layout,\
often using English {} where other languages use {}
""", ["readable", "visual", "keywords", "punctuation"], "Easy"),
("""\
Python interpreters are available for many {}\
allowing Python code to run on a wide variety of systems.\
{} the reference implementation of Python, is {}\
software and has a community-based development model, as\
do nearly all of its variant implementations. {} is managed by the non-profit {}
""", ["operating systems", "cpython", "open source", "python software foundation"], "Medium"),
("""\
Python features a {} system and automatic {} and\
supports multiple {} including object-oriented,\
imperative, functional programming, and\
{} styles. It has a large and comprehensive standard library.
""", ["dynamic type", "memory management", "programming paradigms", "procedural"], "Hard")
]
#Answer and quiz assignment
def assign():
while True:
user_input = raw_input("Select a difficulty, Press 1 for Easy, 2 for Medium or 3 for Hard.\n")
if user_input == "1":
return quizzes[0]
elif user_input == "2":
return quizzes[1]
elif user_input == "3":
return quizzes[2]
else:
print "Error: You must select 1, 2 or 3.\n"
continue
break
def run_quiz():
n = 0
#Declaration of variables
blank = ["___1___", "___2___", "___3___", "___4___"]
tries = 5
print "Welcome to the Python Quiz! This quiz follows a fill in the blank structure. You will have 5 tries to replace the 4 blanks on the difficulty you select. Let's begin!\n"
quiz, answers, difficulty = assign()
print "You have selected {}.\n".format(difficulty)
print "Read the paragraph carefully and prepare to provide your answers.\n"
while n < 4 and tries > 0:
print quiz.format(*blank)
user_input = raw_input("What is your answer for {}? Remember, you have {} tries left.\n".format(blank[n], tries))
if user_input.lower() == answers[n]:
print "That is correct!\n"
blank[n] = answers[n]
n += 1
else:
print "That is the wrong answer. Try again!\n"
tries -= 1
if n == 4 or tries == 0:
if n == 4:
print "Congratulations! You are an expert on Python!"
else:
print "You have no more tries left! You can always come back and play again!"
run_quiz()
,它几乎在任何剧本中都是非常有用的功能。
我还更新了你的代码,不使用全局变量,因为这通常是不好的做法,可能会导致混乱,难以跟踪的错误。它也可能会损害整洁的视觉布局:)。这是您修改后的代码,现在应该可以使用了:
"start of string " + str(var) + " end of string"
关于字符串插值的更多内容:
你做了很多"start of string {} end of string".format(var)"
。这可以通过str
非常简单地实现 - 它甚至可以自动执行quiz
转换。我已将"{}"
变量更改为"__1__"
,其中应显示quiz.format(*blank*)
等或用户的答案。然后,您可以*
打印测验的“最新”版本。 format
此处将空白元素“解包”为format
的单独参数。
如果您发现使用示例用法更容易学习,则在更简单的上下文中有两个>>> "the value of 2 + 3 is {}".format(2 + 3)
'the value of 2 + 3 is 5'
>>> a = 10
>>> "a is {}".format(a)
'a is 10'
用法:
list
我还在[{1}} tuple
个return
中存储了有关每个测验的信息,并且现在分配的值为quizzes
,而不是导致副作用。除此之外,您的代码仍然完好无损。你的原始逻辑根本没有改变。
关于对象的评论:
从技术上讲,是的,2
是一个对象。但是,由于Python是一种“纯面向对象语言”,Python中的所有都是一个对象。 "abc"
是一个对象。 [1, 2, 3]
是一个对象。 quizzes
是一个对象。甚至函数都是对象。您可能会考虑JavaScript - 使用所有括号和括号,它类似于JS对象。但是,class ...
只不过是一个列表(元组)。您可能也在考虑自定义类的实例,但它也不是其中之一。实例要求您首先使用quizzes
定义类。
关于quizzes
实际上是什么 - 它是一个字符串元组列表,字符串和字符串列表。这是一种复杂的类型签名,但它确实只是很多嵌套容器类型。它首先意味着quiz[n]
的每个元素都是'元组'。元组与列表非常相似,只是它不能在适当的位置更改。实际上,你几乎总是可以使用列表而不是元组,但我的经验法则是异构集合(意思是不同类型的东西)通常应该是一个元组。每个元组都有测验文本,答案和难度。我把它放在这样的对象中,因为它意味着可以通过索引(使用quiz1
)来访问它,而不是通过一堆if语句来引用quiz2
,{{1}通常,如果你发现自己命名的变量超过两个在语义上类似的变量,那么将它们放在一个列表中是个好主意,这样你就可以索引和迭代等。
答案 1 :(得分:0)
现在我才能正确阅读你的问题。 你首先使你的字符串quiz1,quiz2一个quiz3。 你只做了一次。
之后你改变你的空白阵列。 但是你不能重建你的弦乐。 所以他们仍然有旧的价值观。
注意,空白数组的元素的副本被制作成例如: quiz1。 事实之后,该副本不会自动更改。 如果你想这样编程,你每次更改空白数组时都必须明确重建quiz1,quiz2和quiz3字符串。
一般建议:不要使用这么多全局变量。请改用函数参数。但是对于第一次尝试,我猜它没关系。
[编辑]
一个简单的修改是:
用函数get_quiz(),get_quiz1()等替换你的测验,测验1,测验2和测验3,获取最新版本,包括空白的改变元素。
这种修改并不能使它成为一个优雅的程序。但是你会有更多的经验来实现这一目标。
如果你想知道(但不要试图在一步中弥合这个差距),那就是一个长镜头: 最后,Quiz可能是一个具有方法和属性的类,其中有实例。
可以肯定的是:我认为像这样的实验会让你成为一名优秀的程序员,而不仅仅是复制一些准备好的代码!