Python for循环和if语句的复杂问题
Python complex problem of for loop & if statements
我需要您的帮助,因为我是编程新手,因此我的知识仅限于出于自己的兴趣而学到的东西。
基本上我有一个包含以下数据的 excel 文件:
我想对此执行以下逻辑步骤。
- 单元格 C1 将被标记为 "X",即
X=ws.['C1']
。 Y = X-5
& 然后 print('X=' + str(X))
检查单元格 C2 是否小于或等于 Y;
一世。如果是,则 Y=Cell ['C2']
& 然后 print('Y=' +str(Y))
& 现在 X 将是下一个单元格,即 X=ws.['C3']。 Y 将 = 新的 X-5。
& 然后 print('X=' + str(X))
。
再次检查第 2 点中提到的相同条件(循环)。
二。如果否,即 C2>Y,则 Y=Cell[C2]-5。
再次检查第 2 点中提到的条件。
我正在使用以下代码,我知道这是错误的。
import openpyxl
from openpyxl import load_workbook
import datetime
wb = load_workbook('D:/Python/data.xlsx')
ws = wb.active
X=float(ws["C2"].value)
print('X=' +str(X))
Y=float(X - 5)
for row in range(2, ws.max_row + 1):
cell=float(ws['C' +str(row)].value)
if cell < Y:
Y=cell
print('Y='+str(Y))
else:
Y=cell-5
X=float(ws['C' +str(row)+1].value)
print('X=' +str(X))
from openpyxl import load_workbook
work_book = load_workbook("62357026/source.xlsx")
work_sheet = work_book.active
buying_price = work_sheet["C2"].value # Assuming all data are integer.
loss_threshold = buying_price - 5
print(f"Price = {buying_price}\nStarting Step 2:")
for index, row in enumerate(work_sheet.rows):
a, b, c = row # (<Cell 'Sheet1'.Ax>, <Cell 'Sheet1'.Bx>, <Cell 'Sheet1'.Cx>)
print(f'\nrow {index}: {a.coordinate} {b.coordinate} {c.coordinate}')
print(f'row {index}: {a.value} {b.value} {c.value}')
price = row[2].value
if price <= loss_threshold:
loss_threshold = price
print(f"threshold = {loss_threshold}")
else:
buying_price = price
loss_threshold = buying_price - 5
print(f"threshold = {loss_threshold}")
结果:
Price = 81
Starting Step 2:
row 0: A1 B1 C1
row 0: Mango Monday 31
threshold = 31
row 1: A2 B2 C2
row 1: Mango Tuesday 81
threshold = 76
row 2: A3 B3 C3
row 2: Mango Wednesday 89
threshold = 84
row 3: A4 B4 C4
row 3: Mango Thursday 84
threshold = 84
row 4: A5 B5 C5
row 4: Mango Friday 22
threshold = 22
row 5: A6 B6 C6
row 5: Mango Saturday 56
threshold = 51
row 6: A7 B7 C7
row 6: Mango Sunday 53
threshold = 48
row 7: A8 B8 C8
row 7: Mango Monday 94
threshold = 89
Process finished with exit code 0
我需要您的帮助,因为我是编程新手,因此我的知识仅限于出于自己的兴趣而学到的东西。 基本上我有一个包含以下数据的 excel 文件:
我想对此执行以下逻辑步骤。
- 单元格 C1 将被标记为 "X",即
X=ws.['C1']
。 Y = X-5 & 然后print('X=' + str(X))
检查单元格 C2 是否小于或等于 Y;
一世。如果是,则Y=Cell ['C2']
& 然后print('Y=' +str(Y))
& 现在 X 将是下一个单元格,即 X=ws.['C3']。 Y 将 = 新的 X-5。 & 然后print('X=' + str(X))
。 再次检查第 2 点中提到的相同条件(循环)。二。如果否,即 C2>Y,则 Y=Cell[C2]-5。 再次检查第 2 点中提到的条件。
我正在使用以下代码,我知道这是错误的。
import openpyxl
from openpyxl import load_workbook
import datetime
wb = load_workbook('D:/Python/data.xlsx')
ws = wb.active
X=float(ws["C2"].value)
print('X=' +str(X))
Y=float(X - 5)
for row in range(2, ws.max_row + 1):
cell=float(ws['C' +str(row)].value)
if cell < Y:
Y=cell
print('Y='+str(Y))
else:
Y=cell-5
X=float(ws['C' +str(row)+1].value)
print('X=' +str(X))
from openpyxl import load_workbook
work_book = load_workbook("62357026/source.xlsx")
work_sheet = work_book.active
buying_price = work_sheet["C2"].value # Assuming all data are integer.
loss_threshold = buying_price - 5
print(f"Price = {buying_price}\nStarting Step 2:")
for index, row in enumerate(work_sheet.rows):
a, b, c = row # (<Cell 'Sheet1'.Ax>, <Cell 'Sheet1'.Bx>, <Cell 'Sheet1'.Cx>)
print(f'\nrow {index}: {a.coordinate} {b.coordinate} {c.coordinate}')
print(f'row {index}: {a.value} {b.value} {c.value}')
price = row[2].value
if price <= loss_threshold:
loss_threshold = price
print(f"threshold = {loss_threshold}")
else:
buying_price = price
loss_threshold = buying_price - 5
print(f"threshold = {loss_threshold}")
结果:
Price = 81
Starting Step 2:
row 0: A1 B1 C1
row 0: Mango Monday 31
threshold = 31
row 1: A2 B2 C2
row 1: Mango Tuesday 81
threshold = 76
row 2: A3 B3 C3
row 2: Mango Wednesday 89
threshold = 84
row 3: A4 B4 C4
row 3: Mango Thursday 84
threshold = 84
row 4: A5 B5 C5
row 4: Mango Friday 22
threshold = 22
row 5: A6 B6 C6
row 5: Mango Saturday 56
threshold = 51
row 6: A7 B7 C7
row 6: Mango Sunday 53
threshold = 48
row 7: A8 B8 C8
row 7: Mango Monday 94
threshold = 89
Process finished with exit code 0