简体   繁体   English

Python:我已经嵌套了不起作用的循环,我不知道为什么

[英]Python: I have nested for loops that aren't working and I don't know why

So I'm trying to figure out how to print numbers that have more even digits than odd digits. 所以我试图弄清楚如何打印数字比奇数更多的数字。 This is my code: 这是我的代码:

inVal=(input("Please enter a positive integer: "))
evencounter=0
oddcounter=0

for i in range(1,int(inVal)):
    for j in range (1,len(str(i))):
        if(int(str(i)[j])%2==0):
            evencounter+=1
        else:
            oddcounter+=1
    if(evencounter>oddcounter):
        print(i)

What's wrong? 怎么了?

I think that the problem is, you haven't reset your counter variables. 我认为问题是,你还没有重置你的计数器变量。 Another problem is that your second loop starts from 1 instead of 0. (thanks Leonardo Gazdek for pointing out!) 另一个问题是你的第二个循环从1而不是0开始。(感谢Leonardo Gazdek指出!)

Strings indexes start from 0: 字符串索引从0开始:

>>> 'abc'[1]
'b'

Here's how you should do it: 这是你应该怎么做的:

inVal=(input("Please enter a positive integer: "))
evencounter=0
oddcounter=0

for i in range(1,int(inVal)):
    # add those pieces of code:
    evencounter=0 
    oddcounter=0
    for j in range (0,len(str(i))): # start from 0
        if(int(str(i)[j])%2==0):
            evencounter+=1
        else:
            oddcounter+=1
    if(evencounter>oddcounter):
        print(i)

As zeet correctly pointed out, you need to reset the counters on each number (they don't even need to be global), but you have another issue. 正如zeet正确指出的那样,你需要重置每个数字上的计数器(它们甚至不需要是全局的),但是你有另一个问题。 You need to start the second loop from 0, not 1. String indexes start from 0. 您需要从0开始第二个循环,而不是1.字符串索引从0开始。

inVal=(input("Please enter a positive integer: "))

for i in range(1,int(inVal)):
    evencounter=0 
    oddcounter=0
    for j in range (0,len(str(i))):
        if(int(str(i)[j])%2==0):
            evencounter+=1
        else:
            oddcounter+=1
    if(evencounter>oddcounter):
        print(i)

range(start, stop[, step]) gives values from start till values less than stop . range(start, stop[, step])给出从start到值小于 stop值。

In your inner loop, change 在你的内循环中,改变

for j in range (1,len(str(i))):

to

for j in range (0,len(str(i))):

since the indexing starts from 0 . 因为索引从0开始。

And you need to reset the value of the counter after each iteration of the outer loop so that it can start over for the next number. 并且您需要在每次外循环迭代后重置计数器的值,以便它可以重新开始下一个数字。

Also, if you want to include inVal as well, change the outer loop to 此外,如果您还想包含inVal ,请将外部循环更改为

for i in range(1,int(inVal)+1):

You might also want to check this out for exception handling in case the user's input is not a number. 如果用户的输入不是数字,您可能还需要检查异常处理。

evencounter=0
oddcounter=0

try:
    for i in range(1,int(inVal)+1):
        for j in range (0,len(str(i))):
            if(int(str(i)[j])%2==0):
                evencounter+=1
            else:
                oddcounter+=1
        if(evencounter>oddcounter):
            print(i)

        oddcounter = evencounter = 0
except ValueError:
    print('Conversion error!')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM