我对编码环境不熟悉,只是开始学习。作为家庭作业需要做的事情"游戏"代码,它是它的一部分。但我不知道如何在函数code_found()中打印(保存)3个函数(digit_1,digit_2,digit_3 = 1,6,9)的结果。
def unlock():
print('You should enter 3 digit code to unlock the door.')
print("To find the code solve problems.")
digit_1()
def digit_1():
print("\nIn order to find the 1st digit solve math.")
x = int(input("Let's find 1nd digit:\n\t10 - 8 + 2 * 3 / 2 / 2 - 1.5 - 1 = ? > "))
if x == 1:
print(f"Correct! 1st digit of the code is: '{x}'\n")
digit_2()
else:
print("\nWrong! Try it again!\n")
digit_1()
def digit_2():
print("In order to find the 2nd digit solve math.")
y = int(input("Let's find 2nd digit:\n\t( 18 + 8 * 3 / 2 ) / 2 - 9 = ? > "))
if y == 6:
print(f"Correct! 2nd digit of the code is: '{y}'\n")
digit_3()
else:
print("\nWrong! Try it again!\n")
digit_2()
def digit_3():
print("In order to find the 3rd digit solve problems.")
z = int(input("Let's find 3rd digit:\n\t( 36 - 8 * 3 / 2 ) / 3 + 1 = ? > "))
if z == 9:
print(f"Correct! 3rd digit of the code is: '{z}'\n")
code_found()
else:
print("\nWrong! Try it again!\n")
digit_3()
def code_found():
print(f"Well done! 3 digit code is found: {?????} ")
unlock()
答案 0 :(得分:1)
您可以从函数中返回有效结果并记住它们并将它们传递到最后一个。我还优化了你的一些代码:
如果您遵循DRY priciple(不要重复自己),您可以通过创建一个简单地获得"更改"的参数化函数来释放许多与您的程序重复的代码。位作为参数:
它们仅在诸如要打印的内容和被认为是正确的等小细节方面有所不同。你可以把它放到一个函数中,它可以打印出来的东西"和#34;什么是正确的" - 我还在int()
周围进行错误处理,如果输入了非int,则会出错:
def unlock():
print('You should enter 3 digit code to unlock the door.')
print("To find the code solve problems.")
# create a data container for "what to print" and "what is correct"
# I am using a dictionary here, the key goes into the "intput(...) text
# the value tuple provides the "what is correct" and the quizz question
q = {"1st": (1,"10 - 8 + 2 * 3 / 2 / 2 - 1.5 - 1 = ?"),
"2nd": (6,"( 18 + 8 * 3 / 2 ) / 2 - 9 = ?"),
"3rd": (9,"( 36 - 8 * 3 / 2 ) / 3 + 1 = ?")}
# collects the correct results for each question
allSolutions = []
sortedKeys = sorted(q.keys())
for k in sortedKeys: # go over the dict in aplhabetical order of keys
result = ask(k, q[k]) # ask the question until valid
allSolutions.append( result ) # append result for later
code_found(allSolutions) # print the result message
def ask(which,what):
"""Asks user for input until the input equals what[0] ( the solution )"""
solution,quizz = what
while True:
try:
s = int(input(f"Let's find {which} digit:\n\t{quizz} > "))
if s == solution:
break
else:
raise ValueError # wrong answers raise an error to be handled below
except (ValueError, EOFError):
print("\nWrong! Try it again!\n")
print(f"Correct! {which} digit of the code is: '{solution}'\n")
return solution
def code_found(numbers):
print(f"Well done! 3 digit code is found: {''.join(map(str,numbers))}")
unlock()
<强>优势:强>
如果您提供此词典:
q = {"1st": (8,"10 - 2 = ?"),
"2nd": (6,"2 * 3 = ?"),
"3rd": (5,"Smalles prime number greater then 3 = ?"),
"4th": (0,"Whats number looks most alike to a capital O?"),
"5th": (111,"1*100+1*10+1 = ?")}
程序仍然有效。只需使用其他挑战和解决方案 - 无需修改任何其他代码。
整个程序从硬编码转移到数据驱动 - 这会带来更多的灵活性,直到你遇到9个以上的问题。然后你会遇到字母数字而不是数字排序的问题 - 但是这样就过去了。
玩得开心。