总蟒蛇初学者在这里.
我有以下函数,它检查文本文件中是否存在从某些输入派生的字符串.它遍历文本文件的每一行,以查看是否找到了完全匹配.
我必须在找到匹配后立即退出循环,以避免不必要的循环.
这是代码:
def DateZoneCity_downloaded_previously(Order_Date,ZoneCity): # function to check if a given DateZoneCity
# combination had already been completely downloaded
string_to_match = Order_Date.strftime('%Y/%m/%d') + "-" + ZoneCity[0] + "-" + ZoneCity[1]
with open(Record_File) as download_status:
DateZoneCity_exists = False
for line in download_status:
if string_to_match in line:
DateZoneCity_exists = True # if match found, then set "DateZoneCity_exists" to True
break # and break out from the [for line in download_status:] loop
if DateZoneCity_exists: return True
download_status.close()
Run Code Online (Sandbox Code Playgroud)
我正在寻找一种更简洁,更pythonic的方式来构造代码.有什么办法可以让它变得更好吗?以某种方式消除了对"DateZoneCity_exists"和第二个If语句的需求?
我正在尝试以下代码,并反复抛出'else if if'和类似的错误.我只是无法弄清楚错误在哪里,因为每个if都被正确终止了.
这是代码:
Sub hra_macro()
Dim city As Boolean
Dim salary As Integer
Dim varHRA As Integer
Dim mimHRA As Integer
Dim maxHRA As Integer
Dim A As Integer
Dim B As Integer
Dim Rent As Integer
city = Range("b1").Value
salary = Range("b2").Value
Rent = Range("b3").Value
If city = True Then
varHRA = 0
A = Rent - (salary / 10)
B = salary * 0.5
Do While varHRA < salary
varHRA = varHRA + 1
If (varHRA < …Run Code Online (Sandbox Code Playgroud)