I am learning Python via Harvard's CS50 and I am having difficulty in developing a program as described here: https://cs50.harvard.edu/python/2022/psets/4/professor/. The code I've come up with works as expected with all the tests I run through it; however, it fails several check50 tests. Specifically, I get the errors described here: https://submit.cs50.io/check50/241b21748cb55a00e77338eff5aca0e7e3f3a0e1.
我通过哈佛大学的CS50学习Python,我在开发一个程序时遇到了困难,如这里所描述的:https://cs50.harvard.edu/python/2022/psets/4/professor/。我提出的代码在我运行的所有测试中都能按预期工作;但是,它在几个check 50测试中失败了。具体地说,我得到了这里描述的错误:https://submit.cs50.io/check50/241b21748cb55a00e77338eff5aca0e7e3f3a0e1。
Should I hardcode a response that's being expected? I'm not understanding how "Level: X + Y =" is being returned instead of just "X + Y = ".
我是否应该对预期的响应进行硬编码?我不明白如何返回“Level:X+Y=”而不仅仅是“X+Y=”。
Here is my code:
以下是我的代码:
import random
def main():
random.seed()
listOperand1 = []
listOperand2 = []
nLevel = get_level()
for i in range(10):
listOperand1.append(generate_integer(nLevel))
listOperand2.append(generate_integer(nLevel))
nScore = 0
for i in range(10):
nAttempts = 3
while nAttempts > 0:
try:
nAnswer = int(input(f"{listOperand1[i]} + {listOperand2[i]} = "))
except ValueError:
nAttempts -= 1
print("EEE")
continue
finally:
if nAttempts == 0:
print(
f"{listOperand1[i]} + {listOperand2[i]} = {listOperand1[i]+listOperand2[i]}"
)
break
if nAnswer == (listOperand1[i] + listOperand2[i]):
nScore += 1
break
else:
nAttempts -= 1
print("EEE")
print(f"Score: {nScore}")
def get_level():
while True:
try:
nLevel = int(input("Level: ").strip())
if nLevel in [1, 2, 3]:
return nLevel
else:
continue
except ValueError:
continue
def generate_integer(level):
if level == 1:
return random.randint(0, 9)
elif level == 2:
return random.randint(10, 99)
elif level == 3:
return random.randint(100, 999)
else:
raise ValueError
if __name__ == "__main__":
main()
更多回答
It is odd that it's looking for the specific question 6 + 6 =
, when the digits are supposed to be random. I assume the Level:
output is coming from the input prompt where the user chooses the level.
奇怪的是,它在寻找特定的问题6+6=,而数字应该是随机的。我假设级别:输出来自用户选择级别的输入提示符。
优秀答案推荐
Suggest removing random.seed()
. It interferes with the testing black box that is check50.
建议删除随机种子()。它会干扰检查50的测试黑匣子。
更多回答
我是一名优秀的程序员,十分优秀!