python 使用if循环时打印偶数的“count”

bxjv4tth  于 2023-04-10  发布在  Python
关注(0)|答案(5)|浏览(125)

我希望我能用Python完成以下内容...
填写空格完成函数“even_numbers(n)"。该函数应该计算从0到给定的“n”数的序列中存在多少个偶数,其中0计数为偶数。例如,even_numbers(25)应该返回13,even_numbers(6)应该返回4。

def even_numbers(n):
   count = 0
   current_number = 0
   while ___ # Complete the while loop condition
       if current_number % 2 == 0::
           ___ # Increment the appropriate variable
       ___ # Increment the appropriate variable
   return count
   
print(even_numbers(25))   # Should print 13
print(even_numbers(144))  # Should print 73
print(even_numbers(1000)) # Should print 501
print(even_numbers(0))    # Should print 1

我已经尝试了我能想到的每一种变化来改变我能想到的每一个变量。我参考了课程作业(我已经在这个问题上坚持了一个星期,而且还在继续计算),我已经搜索了堆栈溢出,并发现多个变体引用不完全是我需要做的(而且从我现在学习的地方来看,最先进的是……即使他们解决了问题,他们也没有……我还是试了试,希望灯泡会熄灭,但它没有)。
我一直在尝试让print(even_numbers(#))部分匹配起来...使脚本工作。
有没有人可以向我解释这一点而不直接给出答案?我真的很想知道我错在哪里,为什么。我是一个完全的新手,大部分是自学的书籍。
尝试(其中一些)

def even_numbers(n):
    count = 0
    current_numbers = 0
    while n > current_numbers: # Complete the while loop condition
        if current_numbers % 2 == 0:
            count = count + 1 # Increment the appropriate variable
        else:
            current_numbers = current_numbers + 1 # Increment the appropriate variable
    return count
    
print(even_numbers(25))   # Should print 13
print(even_numbers(144))  # Should print 73
print(even_numbers(1000)) # Should print 501
print(even_numbers(0))    # Should print 1

Evaluation took more than 5 seconds to complete.
                Please try again with a simpler expression.
def even_numbers(n):
    count = 0
    current_number = 0
    while n > current_number: # Complete the while loop condition
        if current_number % 2 == 0:
           count = count + 1 # Increment the appropriate variable
        else:
            current_number = current_number + 1 # Increment the appropriate variable
    return count

print(even_numbers(25))   # Should print 13
print(even_numbers(144))  # Should print 73
print(even_numbers(1000)) # Should print 501
print(even_numbers(0))    # Should print 1

Evaluation took more than 5 seconds to complete.
                Please try again with a simpler expression.
def even_numbers(n):
    count = 0
    current_numbers = 1
    while n > current_numbers: # Complete the while loop condition
        if current_numbers % 2 == 0:
           count = count + 1 # Increment the appropriate variable
           current_numbers = current_numbers + 1
        else:
           current_numbers = current_numbers + 1 # Increment the appropriate variable
    return count
    
print(even_numbers(25))   # Should print 13
print(even_numbers(144))  # Should print 73
print(even_numbers(1000)) # Should print 501
print(even_numbers(0))    # Should print 1

12
71
499
0
1qczuiv0

1qczuiv01#

在赋值的代码中没有else:。你需要在每次迭代中递增current_number,而不仅仅是当数字是奇数时。否则当数字是偶数时,你会卡在同一个数字上。所以你不应该添加else:块。
此外,如果n是偶数,则结果应该计数n,因此条件应该是current_number <= n

def even_numbers(n):
    count = 0
    current_number = 0
    while current_number <= n: # Complete the while loop condition
        if current_number % 2 == 0::
            count += 1 # Increment the appropriate variable
        current_number += 1 # Increment the appropriate variable
    return count
nafvub8i

nafvub8i2#

您正在计算从current_number == 0current_number == n的偶数,因此应该将current_number初始化为0,循环条件应该是current_number <= n
修改您的第三次尝试,代码应该是:

def even_numbers(n):
    count = 0
    current_number = 0
    while n >= current_number: # Complete the while loop condition
        if current_number % 2 == 0:
           count = count + 1 # Increment the appropriate variable
           current_number = current_number + 1
        else:
           current_number = current_number + 1 # Increment the appropriate variable
    return count
    
print(even_numbers(25))   # Should print 13
print(even_numbers(144))  # Should print 73
print(even_numbers(1000)) # Should print 501
print(even_numbers(0))    # Should print 1

最后,current_number = current_number + 1行在ifelse分支中,因此可以将其提取出来并消除else

def even_numbers(n):
    count = 0
    current_number = 0
    while current_number <= n: # Complete the while loop condition
        if current_number % 2 == 0:
            count += 1 # Increment the appropriate variable
        current_number += 1 # Increment the appropriate variable
    return count
ergxz8rk

ergxz8rk3#

我喜欢你的最后一次尝试。你需要从0开始current_number,并确保使用大于或等于符号:

def even_numbers(n):
    count = 0
    current_numbers = 0
    while n >= current_numbers: # Complete the while loop condition
        if current_numbers % 2 == 0:
           count = count + 1 # Increment the appropriate variable
           current_numbers = current_numbers + 1
        else:
           current_numbers = current_numbers + 1 # Increment the appropriate variable
    return count
    
print(even_numbers(25))   # Should print 13
print(even_numbers(144))  # Should print 73
print(even_numbers(1000)) # Should print 501
print(even_numbers(0))    # Should print 1
ki1q1bka

ki1q1bka4#

这让我得到了正确的答案

def even_numbers(n):
   count = 0
   current_number = 0
   while current_number <= n:
       if current_number % 2 == 0:
           count += 1
       current_number += 1
   return count
   
print(even_numbers(25))
print(even_numbers(144))
print(even_numbers(1000))
print(even_numbers(0))
dgsult0t

dgsult0t5#

这也是可行的,我绝对会提交这个。

def even_numbers(n):
   count = 0
   current_number = 0
   while not current_number: # Complete the while loop condition
       if current_number % 2 == 0:
           count += n // 2 + 1 # Increment the appropriate variable
       current_number = 1 # Increment the appropriate variable
   return count

相关问题