[英]Call function inside loop is not working in python
That function works fine inside the loop该函数在循环内工作正常
import random
def hello():
count = 0
while count < 5:
count = count + 1
print(str(count + random.randint(1, 30)) + " hello I am inside")
break
count = 0
while count < 5:
count = count + 1
print(str(count) + " Outside")
hello()
Print Ourput:打印输出:
K:\Project\Python\Campaign\venv\Scripts\python.exe K:/Project/Python/Campaign/functionInsideLoop.py
1 Outside
12 hello I am inside
2 Outside
30 hello I am inside
3 Outside
19 hello I am inside
4 Outside
6 hello I am inside
5 Outside
23 hello I am inside
Process finished with exit code 0
But when I try to run it function didn't call repeatedly.但是当我尝试运行它时,函数没有重复调用。 I want to output 1 hello I am inside and increment 1, 2, 3, 4, 5
我想输出1 hello I am inside 并递增 1, 2, 3, 4, 5
def hello():
count = 0
while count < 5:
count = count + 1
print(str(count) + " hello I am inside")
break
count = 0
while count < 5:
count = count + 1
print(str(count) + " Outside")
hello()
Print Ourput:打印输出:
K:\Project\Python\Campaign\venv\Scripts\python.exe K:/Project/Python/Campaign/functionInsideLoop.py
1 Outside
1 hello I am inside
2 Outside
1 hello I am inside
3 Outside
1 hello I am inside
4 Outside
1 hello I am inside
5 Outside
1 hello I am inside
Process finished with exit code 0
The idea is calling a function inside a loop again and again.这个想法是一次又一次地在循环中调用一个函数。 I find when I use break inside the loop the function and call it outside another loop the function didn't change.
我发现当我在循环内使用 break 函数并在另一个循环外调用它时,函数没有改变。
If I give you a real example:如果我给你一个真实的例子:
import sqlite3
connection = sqlite3.connect('../miracle.db')
cursor = connection.cursor()
cursor.execute("SELECT* FROM all_fb_user")
def remove_duplicate():
records = cursor.fetchall()
for record in records:
singleRecord = record
# Find All Duplicate
cursor.execute("SELECT * FROM all_fb_user GROUP BY user_url HAVING COUNT(*) > 1;")
records = cursor.fetchall()
try:
for pk in records[1]:
duplicate_pk = (records[1])[4]
print(duplicate_pk)
cursor.execute("DELETE FROM all_fb_user WHERE PK=?", (duplicate_pk,))
break
break
except:
print(" There are no Duplicate in the list")
break
count = 0
while count < 10:
count = count + 1
print(count)
remove_duplicate()
connection.commit()
connection.close()
That remove_duplicate() find and remove duplicate entry because of break it stops inside the loop.这remove_duplicate()查找并删除,因为休息的重复条目停止内循环。 I new to run this function every iteration.
我每次迭代都会运行这个函数。 I need to call the function again and for removing all duplicates.
我需要再次调用该函数并删除所有重复项。
The code has no error, Logically I am making mistake.代码没有错误,逻辑上我犯了错误。 that is the request to for help.
那是寻求帮助的请求。
The break
statement is causing your code to leave the while
loop. break
语句导致您的代码离开while
循环。 Removing the break
will fix your problem删除
break
将解决您的问题
import random
def hello():
count = 0
while count < 5:
count = count + 1
print(str(count) + " hello I am inside")
count = 0
while count < 5:
count = count + 1
print(str(count) + " Outside")
hello()
The above code when I remove the break
statement should output当我删除
break
语句时,上面的代码应该输出
1 Outside
1 hello I am inside
2 hello I am inside
3 hello I am inside
4 hello I am inside
5 hello I am inside
2 Outside
1 hello I am inside
2 hello I am inside
3 hello I am inside
4 hello I am inside
5 hello I am inside
3 Outside
1 hello I am inside
2 hello I am inside
3 hello I am inside
4 hello I am inside
5 hello I am inside
4 Outside
1 hello I am inside
2 hello I am inside
3 hello I am inside
4 hello I am inside
5 hello I am inside
5 Outside
1 hello I am inside
2 hello I am inside
3 hello I am inside
4 hello I am inside
5 hello I am inside
Is this what you are looking for?这是你想要的?
break inside hello() causing the problem, remove it, it will work as below.在 hello() 内部中断导致问题,将其删除,它将按如下方式工作。
>>> def hello():
... count = 0
... while count < 5:
... count = count + 1
... print(str(count) + " hello I am inside")
...
>>> hello()
1 hello I am inside
2 hello I am inside
3 hello I am inside
4 hello I am inside
5 hello I am inside
And modify second one as below:并修改第二个如下:
>>> count = 0
>>> while count < 5:
... count = count + 1
... print(str(count) + " Outside")
... if count == 1:
... hello()
... else:
... break
...
1 Outside
1 hello I am inside
2 hello I am inside
3 hello I am inside
4 hello I am inside
5 hello I am inside
2 Outside
>>>
I find the solution: you have to pass the parameter inside the function.我找到了解决方案:您必须在函数内部传递参数。 in my case records .
在我的情况下记录。
import sqlite3
connection = sqlite3.connect('../miracle.db')
cursor = connection.cursor()
cursor.execute("SELECT* FROM all_fb_user")
def remove_duplicate(records):
print(count)
for record in records:
singleRecord = record
# Find All Duplicate
cursor.execute("SELECT * FROM all_fb_user GROUP BY user_url HAVING COUNT(*) > 1;")
records = cursor.fetchall()
try:
for pk in records[1]:
duplicate_pk = (records[1])[4]
print(duplicate_pk)
cursor.execute("DELETE FROM all_fb_user WHERE PK=?", (duplicate_pk,))
break
break
except:
print(" There are no Duplicate in the list")
break
count = 0
records = cursor.fetchall()
while count < len(records):
count = count + 1
remove_duplicate(records)
print('count')
connection.commit()
connection.close()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.