标签: while-loop

python while循环中的Continue语句

我是 python 的初学者,我在使用这段代码时遇到了问题:

count = 0

while count <15:
   if count == 5:
      continue
   print(count)
   count += 1
Run Code Online (Sandbox Code Playgroud)

当 count 的值 = 5 时,它会停止循环,就像有一个break语句一样。为什么会这样呢?请帮忙!

python continue break while-loop

0
推荐指数
1
解决办法
2352
查看次数

需要逐行读取文本文件,然后用于搜索 CSV 文件

这里完全是新手..抱歉。

我有一个.txt包含 IP 的文件(逐行):

195.678.333.23
54.221.67.1
32.221.67.2
32.221.67.3
56.221.67.4
32.221.67.5
243.221.67.6
23.221.67.7
34.221.67.8
34.221.67.9
Run Code Online (Sandbox Code Playgroud)

(我需要循环大约200个IP)

我需要读取每一行,然后搜索多个 csv 文件以查找这些 IP,看看它们是否存在。

CSV 是这样的:

3am452525-1;2021-08-25T07:49:03.188353+00:00;195.678.333.23;234.45.78.232;\"pass rscore=16\";bl@hotmail.com resolve=ok reverse=bl@hotmail.com routes= notroutes=alfgdfgd,pr_sdt_Spsdgsgdhidfhgd;sxdgdsgs.hotmail.com;ECDHE-RSA-AES256-GCM-SHA384;NOT;sdgsgm@dunog.df;ENV_RCPT:dgsgsgsg@{hotmial.com,ENV_RCPT_VERIFY:1;ATTACH_NAME:sdgdsgsgsof.pdf,ATTACH_MIME:application/pdf,ATTACH_TYPE:pdf,ATTACH_OEXT:pdf,ATTACH_CORRUPT:0,ATTACH_PROTECT:0,ATTACH_SIZE:20099;;;;;;sdgsg_fhdh_dfhdh;asrsar_asrar;0;0;0;0;0;0;0;df;8.12.0-2107140000;main-2108250046;;;;sdgsg;;;;none;none;rule=sdgsgs action=ssdgsg-sgddsgr Subject default="sdgs $reyey";rule=odfhddh action=add-header dfhdhdReason="$SpamReason";;;;;;SEND_QID:4363636363,SEND_PROFILE:mail,SEND_RCPTS:dhdfhh;1;1;default_uioyuouyo_RcptInOrg;30677;uMwaF_x-00m7xOj-rho8njCPLr_QKYh3;<B8967386-0450-4709-A382-66D68CE4F3A6@rtce-efax1>;"zdgddadyhadyhad";0.283;1;0.856;Fax-Tdhfdm@urydyd;sdhdfhdhdhfd
Run Code Online (Sandbox Code Playgroud)

IP 存在于$3CSV 文件的字段中,但如果我找到 IP,那么我希望将整行作为输出。

有人可以帮忙吗?

我有厌倦的事情,比如:

cat /tmp/IPs_filename.txt | while read line; do grep ${line} csvlog.436334.csv; done

但没有帮助。

谢谢

bash loops while-loop cat do-while

0
推荐指数
1
解决办法
366
查看次数

为什么我的代码不能对列表中负数的值求和?

given_list3 = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7]
total7 = 0
i = 0
while i <= len(given_list3) and given_list3[i] <= 0:
    total7 += givenlist3[i]
    i += 1
print(total7)
Run Code Online (Sandbox Code Playgroud)

该代码产生的结果为 0,我想得到:-2 + -3 + -5 + -7 = -17

python while-loop

0
推荐指数
1
解决办法
56
查看次数

通过无限 while 循环在函数之间传递数据

一直在努力让它工作,因为我无法使用return在不会结束的 while 循环中使用。

简而言之,我在一个函数receive()(无限循环)中的套接字客户端中接收一条消息,并且需要将该传入消息的结果传递给main(). 尝试使用yield,因为我不确定还有什么可以实现这一目标。我创建了另一个函数来尝试yield捕获receive()。

我知道初始消息到达服务器,因为它输出消息,并且我知道客户端正在接收服务器的确认消息,因为它正在打印它。我只是没有运气将该数据传递给main(),因此其余代码将无法正常工作。

我对此很陌生,所以我可能做错了。我应该使用类来更轻松地共享数据,但对它们的掌握还不够。希望使用产量或其他东西可以解决这个问题。

接收函数:

def receive():
    while True:
        try:
            incoming = client.recv(2048).decode(FORMAT)
            if 'RECEIVED' in incoming:
                confirmation = 'confirmed'
                yield confirmation
            print(incoming)
        except:
            print("Connection interrupted.")
            client.close()
            break

#------------
# also tried
#------------
def receive():
    while True:
        try:
            incoming = client.recv(2048).decode(FORMAT)
            if 'RECEIVED:COMPLETE' in incoming:
                confirmation = 'confirmed'
            else:
                confirmation = 'unconfirmed'
            yield confirmation
        except:
            print("Connection interrupted.")
            client.close()
            break

Run Code Online (Sandbox Code Playgroud)

返回函数:

def pass_return(passed_return_value):
    passed_return_value …
Run Code Online (Sandbox Code Playgroud)

python variables if-statement function while-loop

0
推荐指数
1
解决办法
372
查看次数

为什么当我的 char 变量达到 [del] 字符 (127) 值时,我的程序会进入无限循环?

这是我的代码:

#include <iostream>

int main()
{
    char x = 32;
    while (x <= 126) {
        std::cout << x << "\n";
        x += 1;
    }
}
Run Code Online (Sandbox Code Playgroud)

到这里为止,一切顺利,但如果我将代码更改为:

#include <iostream>

int main()
{
    char x = 32;
    while (x <= 127 /* here the "bad" change */ ) {
        std::cout << x << "\n";
        x += 1;
    }
}
Run Code Online (Sandbox Code Playgroud)

为了尝试打印 [del] 字符,我的程序进入无限循环并开始打印许多我不想要的其他字符。为什么?

c++ ascii infinite-loop while-loop

0
推荐指数
1
解决办法
228
查看次数

在 Python Tkinter 中,button.configure() 命令未在 while 循环内运行

我正在尝试根据 while 循环确定的值来配置按钮,turn该值由 while 循环确定game_over

while 循环干扰了 tkinter 的主循环。我似乎无法找到解决这个问题的方法。

这是代码。

from tkinter import *
connect = Toplevel()

col1 = Button(connect, width=10, height=4, text=" ")
col1.grid(row=0, column=1)

game_over = False
turn = 0

while not game_over:
    if turn == 0:
        col1.configure(bg="#FE6869")
        x=input("Enter something")
        if x=="yes":
            game_over=True
        else:
            continue   
    else:       
        col1.configure(bg="#FFFC82")
        x=input("Enter something")
        if x=="no":
            game_over=True
        else:
            continue
    turn += 1
    turn = turn % 2

connect.mainloop()
Run Code Online (Sandbox Code Playgroud)

python tkinter button while-loop

0
推荐指数
1
解决办法
52
查看次数

为什么z变量的输出是19?该代码如何更清晰地工作?

我有以下 C++ 程序;

#include <iostream>

int main() {
    int i=1, j=1, k=1, z=0;
    for (; i <= 10; i++)
        while(j <= 10) {
            j++;
            do {
                k++;
                z++;
            } while(k <= 10);
        }
    std::cout << z << " ";
}
Run Code Online (Sandbox Code Playgroud)

运行时输出为 19,我无法理解为什么会这样。

由于之前的 while(j<=10),我预计返回值为 10。我错过了什么?

c++ while-loop do-while

0
推荐指数
1
解决办法
95
查看次数

为什么这个程序无法正确地将字符写入文件中,而是陷入无限循环?

#include <stdio.h>
#include <stdlib.h>

int main() {
    FILE *fp;
    char ch;

    fp = fopen("sample.txt", "r+");
    if (fp == NULL) {
        printf("Problem accessing the file!");
        exit(1);
    }

    while ((ch = fgetc(fp)) != EOF) {
        if (ch == ' ') {
            fseek(fp, -1l, SEEK_CUR);
            fputc('_', fp);
        }
    }

    fclose(fp);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

在这个程序中,我希望打开的文件中的所有空白字符都替换为下划线字符。但这个程序的行为方式非常出乎意料,并陷入了无限循环。我怎样才能让它正常工作?

我尝试fp在 while 循环的每次迭代中刷新流,但它仍然没有给出预期的输出。

c file-handling while-loop

0
推荐指数
1
解决办法
69
查看次数

如何在 python 中获取 While 循环的最后一个元素?

问题是我得到了 while 循环中的所有元素而不是最终结果。有人知道如何让 python 仅在终端中插入最后一个元素吗?

我尝试插入以下循环的结果:从 74 中减去 6,42 次。我使用以下代码得到的结果应该是 -178:

i = 74
while i > -180:
    print(i)
    i = i - 6
Run Code Online (Sandbox Code Playgroud)

我希望将结果打印在一个元素(最后一个元素)中,而不是打印到 -178 的所有数字。

python while-loop python-3.x

0
推荐指数
1
解决办法
229
查看次数

使 while 循环更快的最佳方法是什么?

我有一个计算 1/n 之和的脚本。它可以在 20 分钟内完成 10.000.000.000。但我认为还可以更好。

while i <= n:
    b = 1/i
    h = h+b
    i = 1+i
Run Code Online (Sandbox Code Playgroud)

我实在想不出比从 scrapch 重写代码更好的方法了。任何建议都有帮助。

python while-loop

0
推荐指数
1
解决办法
86
查看次数