python中的while循环不退出

发布于 2025-01-02 12:11:09 字数 615 浏览 0 评论 0原文

我现在正在尝试自学 Python,并且正在使用“Learn Python The Hard Way”中的练习来做到这一点。

现在,我正在做一个涉及 while 循环的练习,我从脚本中获取一个工作 while 循环,将其转换为函数,然后在另一个脚本中调用该函数。最终程序的唯一目的是将项目添加到列表中,然后打印列表。

我的问题是,一旦我调用该函数,嵌入式循环就会决定无限继续。

我已经多次分析了我的代码(见下文),但找不到任何明显的错误。

def append_numbers(counter):
    i = 0
    numbers = []

    while i < counter:
        print "At the top i is %d" % i
        numbers.append(i)

        i += 1
        print "Numbers now: ", numbers
        print "At the bottom i is %d" % i

count = raw_input("Enter number of cycles: ")

print count
raw_input()

append_numbers(count)

I'm trying to teach myself python right now, and I'm using exercises from "Learn Python The Hard Way" to do so.

Right now, I'm working on an exercise involving while loops, where I take a working while loop from a script, convert it to a function, and then call the function in another script. The only purpose of the final program is to add items to a list and then print the list as it goes.

My issue is that once I call the function, the embedded loop decides to continue infinitely.

I've analyzed my code (see below) a number of times, and can't find anything overtly wrong.

def append_numbers(counter):
    i = 0
    numbers = []

    while i < counter:
        print "At the top i is %d" % i
        numbers.append(i)

        i += 1
        print "Numbers now: ", numbers
        print "At the bottom i is %d" % i

count = raw_input("Enter number of cycles: ")

print count
raw_input()

append_numbers(count)

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

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

发布评论

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

评论(4

情绪失控 2025-01-09 12:11:09

我相信你想要这个。

count = int(raw_input("Enter number of cycles: "))

如果不将输入转换为整数,您最终会在 count 变量中得到一个字符串,即,如果您在程序要求输入时输入 1,则进入 count 的是 <代码>'1'。

字符串和整数之间的比较结果为False。所以 while i 中的条件counter: 始终为 False,因为在程序中,i 是整数,而 counter 是字符串。

在您的程序中,如果您使用 print repr(count) 来检查 count 变量中的值,您可以自己调试它。对于您的程序,当您输入 1 时,它会显示 '1'。通过我建议的修复,它只会显示 1

I believe you want this.

count = int(raw_input("Enter number of cycles: "))

Without converting the input to integer, you end up with a string in the count variable, i.e. if you enter 1 when the program asks for input, what goes into count is '1'.

A comparison between string and integer turns out to be False. So the condition in while i < counter: is always False because i is an integer while counter is a string in your program.

In your program, you could have debugged this yourself if you had used print repr(count) instead to check what the value in count variable is. For your program, it would show '1' when you enter 1. With the fix I have suggested, it would show just 1.

桃扇骨 2025-01-09 12:11:09

将输入字符串转换为整数... count=int(count)

convert the input string to integer... count=int(count)

以可爱出名 2025-01-09 12:11:09

上面已经解释了为什么 while 永远循环。它循环了很多次(=ASCII 代码非常大)

但是要修复 while 你可以简单地:

while i<int(counter):
    print "At the top i is %d" % i
    numbers.append(i)

Above has been cleared why the while looped for ever. It loops for a lot(=ASCII code which is really big)

But to fix the while you can simply:

while i<int(counter):
    print "At the top i is %d" % i
    numbers.append(i)
水溶 2025-01-09 12:11:09

raw_input 返回一个字符串,但 i 是一个整数。尝试使用 input 而不是 raw_input

raw_input returns a string, but i is an integer. Try using input instead of raw_input.

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