程序必须向用户询问均匀的整数最大六次,如果没有结束

发布于 2025-02-01 19:10:32 字数 310 浏览 4 评论 0原文

该程序向用户询问一个整数。如果他要求6次,该程序结束了。如果数字是,它将返回它。到目前为止,我的代码:

i = 0
for i in range(6):
    num = int(input("Num: "))
    if num % 2 == 0:
        print(num)
    else:
        num = int(input("Num: "))
        i+=1
        if i == 6:
            break

我的程序在用户给出6个什至没有数字后没有结束,我该如何解决?

The program ask the user for an even integer. If he asks for it 6 times, the program ends. If the number is even, it returns it. My code so far:

i = 0
for i in range(6):
    num = int(input("Num: "))
    if num % 2 == 0:
        print(num)
    else:
        num = int(input("Num: "))
        i+=1
        if i == 6:
            break

My program doesn't end after user gives 6 not even numbers, how can i fix this?

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

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

发布评论

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

评论(2

丑疤怪 2025-02-08 19:10:32

您无需检查i == 6自己,这是由循环自动完成的。

但是,当输入偶数时,您需要脱离循环。

for i in range(6):
    num = int(input("Num: "))
    if num % 2 == 0:
        print("Success!")
        break
else:
    print("Failure")

else:如果循环得出其正常结论,而不是由于break而退出,则执行循环的块。

You don't need to check for i == 6 yourself, this is done automatically by the for loop.

But you need to break out of the loop when an even number is entered.

for i in range(6):
    num = int(input("Num: "))
    if num % 2 == 0:
        print("Success!")
        break
else:
    print("Failure")

The else: block of a loop is executed if the loop reaches its normal conclusion instead of exiting due to break.

玩心态 2025-02-08 19:10:32
for i in range(6):
    num = int(input("Num: "))
    if num % 2 == 0:
        print(liczba)
        break

如果没有所有额外的线条,这将完成您想要的事情。

for i in range(6):
    num = int(input("Num: "))
    if num % 2 == 0:
        print(liczba)
        break

This will do what you want, without all the extra lines you had.

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