哈佛CS50P pset 4:等待程序退出时计时

发布于 2025-02-03 19:28:43 字数 1700 浏览 1 评论 0原文

我正在求解这个问题

我的代码在测试时起作用,但它正在失败所提供的检查错误:

:(小教授接受有效的水平//等待程序退出时

它通过了以前的三个检查级别的检查。任何有关发生这种情况可能会有所帮助的见解。

这是我的代码:

def main():
    level = get_level()
    q=1
    s=0
    while q<=10:
        try:
            x = generate_integer(level)
            y = generate_integer(level)
            ans = x + y
            eq = int(input(f'{x} + {y} = '))
        
            if ans == eq:
                s += 1
                pass
            
            elif ans != eq:
                c = 1
                while c <= 2:
                    print('EEE')
                    eq = int(input(f'{x} + {y} = '))
                    
                    if ans == eq:
                        s += 1
                        break

                    else:
                        c += 1
                        
                if c == 3:
                    print('EEE')
                    print(f'{x} + {y} = {ans}')
            q += 1
            
        except EOFError:
            pass
        
    print(f'Score: {s}')
    
def get_level(): 
    valid_inputs = (1,2,3)
    while True:
        try:
            level = int(input('Level: '))
            if level in valid_inputs:
                return level
            else:
                pass
        except ValueError:
            pass
        

def generate_integer(level):
    max = 10**level-1
    min = 10**(level-1)
    
    if level == 1:
        min == 0
    
    num = random.randint(min,max)
    
    return num

main()

I'm solving this problem.

My code works when I test it but it is failing the provided check with the error:

:( Little Professor accepts valid level // timed out while waiting for program to exit

It passes the three previous checks for invalid levels. Any insight to why this might be happening would help.

Here is my code:

def main():
    level = get_level()
    q=1
    s=0
    while q<=10:
        try:
            x = generate_integer(level)
            y = generate_integer(level)
            ans = x + y
            eq = int(input(f'{x} + {y} = '))
        
            if ans == eq:
                s += 1
                pass
            
            elif ans != eq:
                c = 1
                while c <= 2:
                    print('EEE')
                    eq = int(input(f'{x} + {y} = '))
                    
                    if ans == eq:
                        s += 1
                        break

                    else:
                        c += 1
                        
                if c == 3:
                    print('EEE')
                    print(f'{x} + {y} = {ans}')
            q += 1
            
        except EOFError:
            pass
        
    print(f'Score: {s}')
    
def get_level(): 
    valid_inputs = (1,2,3)
    while True:
        try:
            level = int(input('Level: '))
            if level in valid_inputs:
                return level
            else:
                pass
        except ValueError:
            pass
        

def generate_integer(level):
    max = 10**level-1
    min = 10**(level-1)
    
    if level == 1:
        min == 0
    
    num = random.randint(min,max)
    
    return num

main()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泪之魂 2025-02-10 19:28:43

我运行了您的程序(AS-I),发现至少有1个问题。我不确定这是否是为什么check50标记该错误的原因,但是您需要在它通过之前对其进行修复。我输入级别:1,然后获得了重复方程(4 + 5 =)。这违反了如何测试的要求:“ 您的程序应输出 10截然不同的问题问题”(重点是我的)。很容易错过 - 直到有人在CS50P ED论坛上提到它之前,我才看到它。

同样,程序的最后2行应该是:

if __name__ == "__main__":
    main()

相反,只有您有:

main()

最后,这是对您的代码结构的观察。如果语句是3个尝试回答问题的语句,则有很多。它过于复杂,很难遵循。提示:可以使用另一个循环简化并检查答案。

I ran your program (as-is) and found at least 1 problem. I'm not sure if it's why check50 flags that error, but you will need to fix it before it will pass. I entered Level: 1, then got a duplicate equation (4 + 5 = ). This violates a requirement buried under How to Test: "Your program should output 10 distinct problems" (emphasis mine). It's easy to miss - I didn't see it until someone mentioned it on the CS50P ED Forum.

Also, the last 2 lines of your program are supposed to be:

if __name__ == "__main__":
    main()

Instead, only you have:

main()

Finally, this is an observation on your code structure. You have a lot of if statements for the 3 attempts to answer the question. It's overly complicated and kind of hard to follow. Hint: It can be simplified with another loop to prompt and check answers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文