For Loop Not Breaking(Python) [英] For Loop Not Breaking (Python)

查看:107
本文介绍了For Loop Not Breaking(Python)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Python中编写一个简单的For循环。有没有办法打破循环,而不使用break命令。我认为通过设置count = 10即可满足退出条件,循环将停止。但似乎并非如此。

注意:部分挑战是使用FOR循环,而不是WHILE循环。

  import random 

guess_number = 0
count = 0
rand_number = 0

rand_number = random.randint(0,10)

print(猜测的数字是,rand_number)

(0,5):
guess_number = int(输入(输入0-10之间的任意数字))
if guess_number == rand_number:
print(你猜对了! )
count = 10
else:
print(Try again ...)
count + = 1

我是编程新手,所以我只是弄湿了脚。我可以使用休息,但我想弄清楚为什么循环没有结束,当你输入正确的猜测数字。 就像在Java和C等其他编程语言中看到的一样。 range(0,5)会生成一个列表,而用于循环遍历它。循环的每次迭代都没有检查条件。因此,你可以将循环变量重新分配给你的心愿,但是在下一次迭代时,它将被设置为列表中下一个值。

真的不会无论如何,这是无意义的,因为你可以遍历任意列表。如果你的列表是 range(0,5),比如 [1,3,-77,'Word',12, '你好'] ?没有办法重新分配变量的方式是有意义的,打破循环。

我可以想到三种合理的方式来打破循环:

p>


  1. 使用 break 语句。这使您的代码保持清洁,易于理解。
  2. 在try-except块中环绕循环并引发异常。这不适合你在这里显示的例子,但是这是一种你可以跳出一个(或多个)循环的方式。
  3. >
  4. 将代码放入一个函数中,并使用 return 语句突破。这也允许你为循环分出多个

另一种方法(至少在Python 2.7中)可以从循环中分离出来,就是使用现有的列表,然后在迭代过程中对其进行修改。 请注意,这是一个非常糟糕的方式,但它的工作原理。我不确定这个例子是否可以在Python 3.x中工作,但是可以在Python 2.7中使用:

  iterlist = [1,2,3,4] 
我在iterlist中:
doSomething(i)
如果我== 2:
iterlist [:] = []

如果您有 doSomething 打印出 i ,它只打印出1和2,然后退出循环,没有错误。同样,这是一个不好的方法


I'm writing a simple For loop in Python. Is there a way to break the loop without using the 'break' command. I would think that by setting count = 10 that the exit condition would be met and the loop would stop. But that doesn't seem to be the case.

NOTE: Part of the challenge is to use the FOR loop, not the WHILE loop.

import random

guess_number = 0 
count = 0
rand_number = 0

rand_number = random.randint(0, 10)

print("The guessed number is", rand_number)    

for count in range(0, 5):
    guess_number = int(input("Enter any number between 0 - 10: "))
    if guess_number == rand_number:
        print("You guessed it!")
        count = 10
    else:
        print("Try again...")
        count += 1

I'm new to programming, so I'm just getting my feet wet. I could use a 'break' but I'm trying figure out why the loop isn't ending when you enter the guessed number correctly.

解决方案

The for loop that you have here is not quite the same as what you see in other programming languages such as Java and C. range(0,5) generates a list, and the for loop iterates through it. There is no condition being checked at each iteration of the loop. Thus, you can reassign the loop variable to your heart's desire, but at the next iteration it will simply be set to whatever value comes next in the list.

It really wouldn't make sense for this to work anyway, as you can iterate through an arbitrary list. What if your list was, instead of range(0,5), something like [1, 3, -77, 'Word', 12, 'Hello']? There would be no way to reassign the variable in a way that makes sense for breaking the loop.

I can think of three reasonable ways to break from the loop:

  1. Use the break statement. This keeps your code clean and easy to understand
  2. Surround the loop in a try-except block and raise an exception. This would not be appropriate for the example you've shown here, but it is a way that you can break out of one (or more!) for loops.
  3. Put the code into a function and use a return statement to break out. This also allows you to break out of more than one for loop.

One additional way (at least in Python 2.7) that you can break from the loop is to use an existing list and then modify it during iteration. Note that this is a very bad way to it, but it works. I'm not sure that this will this example will work in Python 3.x, but it works in Python 2.7:

iterlist = [1,2,3,4]
for i in iterlist:
    doSomething(i)
    if i == 2:
        iterlist[:] = []

If you have doSomething print out i, it will only print out 1 and 2, then exits the loop with no error. Again, this is a bad way to do it.

这篇关于For Loop Not Breaking(Python)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆