我有下面的python代码,可以从outlook收件箱的COC子文件夹中下载附件。代码工作正常,但下载完所有附件后不会停止。我怎么才能修好它。
import win32com.client
import os
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox=outlook.GetDefaultFolder(6).folders("COC") # "6" refers to the index of a f
count = 1
i = 3
while count != 1000:
for k in range(2,i):
if i%k == 0:
break
else:
print(i)
count += 1
i += 2
在这段python代码中,如果执行break,程序将跳到添加i += 2,不是应该先执行else吗?
假设else没有缩进到同一级别的if,因此不会成为for循环的一部分。
我目前正在努力学习蟒蛇。我在看艾尔·斯威加特的“用Python自动完成无聊的事情”。在他的while循环示例中,他在while循环中使用了一个not条件(如下面的代码所示)。
name = ''
while not name != '':
print('Enter your name:')
name = input()
print('How many guests will you have?')
numOfGuests = int(input())
if numOfGuests !=0:
print(
我得到一个错误: Traceback (most recent call last):
File "c:/Users/Owen/Desktop/Python/BlackJack.py", line 281, in <module>
main()
File "c:/Users/Owen/Desktop/Python/BlackJack.py", line 230, in main
while money > 0:
TypeError: '>' not supported between instan
在python中的while循环中工作吗?我似乎不能让它起作用。这是我的代码如何工作的示例。
newslot = 3
moved = False
while newslot > 0 or moved != True:
enabled = query something on the database where slot = newslot
if enabled:
print 'do something here'
moved = True
else:
newslot-=1
prin
我试图从数据库代码中获取数据:
$carsql = "SELECT history.partno
FROM history
WHERE history.vm = 'TT'
AND history.status = 0
AND history.voucherno = '$voucherno'";
$carinfo = odbc_exec($db,$carsql);
if($car1 = odbc_fetch_arr
我用python编写了下面的函数,它返回一个子列表(即它一直在迭代的列表的一部分,直到它找到一个特定值(在我的例子中是5),但没有把它包括在子列表中,也停止了进一步的迭代)。
def sublist(a):
b=[]
i=0
while(a[i]!=5 and i<=len(a)-1):
b.append(a[i])
i=i+1
return b
上面的函数在所有测试用例中都工作得很好,除了这个a=[1, 6, 2, 3, 9]。它给出了以下错误:IndexError: list index out of range。
但是
我正在尝试将特定的JavaScript代码转换为Python代码。有一点我不明白。这是JavaScript代码:
do {
// something
} while (a > 0.01);
b = 10
我尝试通过以下方式复制它:
for i in range(1000): # or: while True:
// something
if (a > 0.01):
continue
else:
break
b = 10
但看起
我正在尝试区分和修改整数列表,以确定是小字符串还是大字符串。但是我在python函数中执行while循环有困难。但是通过使用for loop语句,代码就可以运行了。只有当我使用while循环时,它才会给我一个错误。
int_list = [1,2,3,8,0,9]
def str_replace(int_list, index = 0):
while True:
if int_list[index] < 5:
int_list[index] = "small"
index = index + 1
我正在Python中循环一个字符串,在列表中添加";“之间的单词(我知道在Python中还有其他循环字符串的方法,但我希望这样做是可行的):
data = "ABC;AB;AB"
data_len = len(data)
items = []
separator = ";"
i = 0
while i < data_len:
item = ''
if i == 0:
while data[i] != separator:
item += data[i]
from math import log10, exp
places = int(input('Enter the number of decimal places: '))
one = 10**places
extra = 10**4
n = 1
term = one * extra
eee = 0
count = 0
while term > 0:
eee += term
count += 1
term = term // n
n += 1
eee = eee // extra
intPart = eee // one
这是我见过的最简单的代码。我的Python机器人不工作了。不起作用的是,当/start命令被发送到机器人时,它没有开始做任何事情。从逻辑上讲,它必须启动while循环-它不会。它甚至没有从函数FirstNum()开始。这个想法是在循环中使用If - Else语句。 有什么建议吗?我是python编程的初学者,所以不要苛刻地评判我) @bot.message_handler(commands=['start'])
def Main(message):
a = 0
b = 0
c = 0
while a + b + c > 0:
if a == 0:
我正在学习Python,我应该在显示“你输了”之前给用户三个猜测。我认为我的代码允许3个条目,但网站显示"Oops,再试一次!您允许用户进行3次猜测,还是您错误地检测到了正确的猜测?“除非用户在3次试验中猜对了。有人能告诉我怎么回事吗?
from random import randrange
random_number = randrange(1, 10)
count = 0
# Start your game!
guess= int(raw_input("Please type your number here:"))
while count < 2:
我有一个python程序,它打开一个包含大约30000条记录的dbf文件,我想结束我的while循环,但是由于某种原因,我的while循环仍然打开。
with DBF('data.DBF',recfactory=None,load =True) as table:
for row in table:
stono = (row[1][1])
payrollid = (row[15][1])
busdate = (row
我在Python、list1和下面的while循环中有一个列表:
j = 0
while list1[j] >= list1[j - 1] and j < len(list1):
# do something here and return k
# k is always incremented
j += k
我得到了以下错误:IndexError: string index out of range
如何修复此错误?
我不能完全理解为什么会这样:
while True:
age = int(raw_input("What is your age? "))
if age >= 21:
print "Okay! You are old enough to drink."
break
else:
print "Bzzt! You are not old enough to drink."
有效,但这不起作用:
while False:
age = int(raw_in
尝试为基于服务的小费计算器编写python代码时,我遇到了while循环的问题。
service = ""
while service != "excellent" or service != "good" or service != "bad":
service = input("(Please choose excellent, good, or bad): ")
这部分导致了无限循环,但我不确定为什么或如何修复它……
我想知道为什么在python中,当尝试: count = 0
while count < 3:
for i in range(40):
count += 1 并不实际检查while循环条件。而在 count = 0
for i in range(40):
while count < 3:
count += 1 确实如此。一开始,我假设在迭代完成之前,可能必须忽略while循环。但是如果我运行两个不同的for循环 count = 0
while count < 3:
for i in range(40):
我需要用Python编写一段代码,使用while循环返回列表中的第一个正数。这就是我所拥有的,但它不工作。
def positive(num):
n= num[0]
### Can't change anything above
首先,我尝试了:
i = 0
while n <= 0:
i += 1
但这并不管用,所以我试了一下:
i = 0
while i < len(num):
n = num[i]
while n <= 0:
i += 1
###
在这个特定的项目中:
from __future__ import print_function
import random
# Make a list of words
words = ['apple','banana','orange','coconut','strawberry','lime','grapefruit','lemon','kumquat',
'blueberry','melon']
while
你好,我正在尝试做一个循环,它不断地搜索一个类,如果它发现它需要刷新,直到它找不到它,然后它运行其余的代码。我正在使用selenium和python的帮助将是非常感谢的。提前感谢
foundButton = False
while not foundButton:
driver.find_element(By.CLASS_NAME, "buy")
if (driver.find_element(By.CLASS_NAME, "buy")):
time.sleep(1)
刚开始学习Python,但我想不出背后的逻辑,我希望有人能澄清。
# this works without a counter
Numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
for Numbers in Numbers:
print(Numbers)
但
# this doesn't work without a counter
Numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
while Numbers in Numbers <= 5:
print(Numbers)
所以'for循环
我对编程和Python都很陌生。现在有几次,我创建了一个看起来很笨拙的程序流,我想知道我是否遵循了最佳实践。从概念上讲,这是我想要做的:
def pseudocode():
while some condition is true:
do some stuff
if a condition is met:
break out of the while loop
now do a thing once, but only if you never broke out of the loop above
我最终做的事情很管用
我正在尝试制作一个函数,你输入选项,用户选择一个。 Python总是在格式所在的第6行和第9行显示错误消息。 我有python 3.7.4 def choice(choices, message):
print(message)
print("Do you want to :")
answer = ""
for i, x in enumerate(choices):
print(f"{i}: {x}")
while answer > len(choices):
whi
我正在学习Python。对于代码:
def main():
fileName = raw_input("file name ")
infile = open(fileName, "r")
sm = 0.0
ct = 0
line = infile.readline()
while line != "":
sm = sm + eval(line)
ct = ct + 1
line = infile.readline()
print "\nAverage is ", sm/ct
main()
它会导致以下错
假设我有这样的密码:
import time
try:
while True:
print("Hello World")
time.sleep(10)
except:
print("Ctrl+z was pressed") #Doesn't get executed
当我试图在python 3中执行这段代码时,我的not块中的内容不会执行。我能做什么?