小编Chi*_*mat的帖子

Pythonic打破循环的方法

总蟒蛇初学者在这里.

我有以下函数,它检查文本文件中是否存在从某些输入派生的字符串.它遍历文本文件的每一行,以查看是否找到了完全匹配.

我必须在找到匹配后立即退出循环,以避免不必要的循环.

这是代码:

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语句的需求?

python loops python-3.x

4
推荐指数
1
解决办法
453
查看次数

VBA抛出'else without 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)

excel vba if-statement

2
推荐指数
1
解决办法
2719
查看次数

标签 统计

excel ×1

if-statement ×1

loops ×1

python ×1

python-3.x ×1

vba ×1