[英]Call function inside loop is not working in python
该函数在循环内工作正常
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()
打印输出:
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
但是当我尝试运行它时,函数没有重复调用。 我想输出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()
打印输出:
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
这个想法是一次又一次地在循环中调用一个函数。 我发现当我在循环内使用 break 函数并在另一个循环外调用它时,函数没有改变。
如果我给你一个真实的例子:
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()
这remove_duplicate()查找并删除,因为休息的重复条目停止内循环。 我每次迭代都会运行这个函数。 我需要再次调用该函数并删除所有重复项。
代码没有错误,逻辑上我犯了错误。 那是寻求帮助的请求。
break
语句导致您的代码离开while
循环。 删除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()
当我删除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
这是你想要的?
在 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
并修改第二个如下:
>>> 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
>>>
我找到了解决方案:您必须在函数内部传递参数。 在我的情况下记录。
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.