Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python If/else在for循环中_Python_Loops_If Statement - Fatal编程技术网

Python If/else在for循环中

Python If/else在for循环中,python,loops,if-statement,Python,Loops,If Statement,如果eventName等于某个值,如何阻止所有数据追加到data1。但是,如果eventName不等于某个值,我希望循环继续 for event in eventid: request2 = requests.get(f'https://www.punters.com.au/api/web/public/Odds/getOddsComparisonCacheable/?allowGet=true&APIKey=65d5a3e79fcd603b3845f0dc7c2437f0&am

如果
eventName
等于某个值,如何阻止所有数据追加到
data1
。但是,如果
eventName
不等于某个值,我希望循环继续

for event in eventid:
    request2 = requests.get(f'https://www.punters.com.au/api/web/public/Odds/getOddsComparisonCacheable/?allowGet=true&APIKey=65d5a3e79fcd603b3845f0dc7c2437f0&eventId={event}&betType=FixedWin', headers={'User-Agent': 'Mozilla/5.0'})
    json2 = request2.json()
    eventname = json2['eventName']
    for selection in json2['selections']:
        for price in selection['prices']:
            if price['bookmaker'] in ['BetEasy', 'Neds', 'Sportsbet'] and price['hasOdds']:
                data1.append((eventname, selection['name'], price.get('bookmaker'), price.get('odds', 0)))
您正在搜索语句:

if eventname == 'ceratin value':
    break  # stops loop
如果只想跳过附加值,请使用常规条件。

尝试以下操作:

for event in eventid:
    request2 = requests.get(f'https://www.punters.com.au/api/web/public/Odds/getOddsComparisonCacheable/?allowGet=true&APIKey=65d5a3e79fcd603b3845f0dc7c2437f0&eventId={event}&betType=FixedWin', headers={'User-Agent': 'Mozilla/5.0'})
    json2 = request2.json()
    eventname = json2['eventName']
    for selection in json2['selections']:
        for price in selection['prices']:
            if price['bookmaker'] in ['BetEasy', 'Neds', 'Sportsbet'] and price['hasOdds'] and eventname != "certain_value":
                data1.append((eventname, selection['name'], price.get('bookmaker'), price.get('odds', 0)))

if eventname=='sature value':break
他说他不想追加数据,但想继续循环。你的回答将打破循环。请confirm@hpollock然后用这个条件把你的内部循环括起来。@hpollock,不要用他的答案,这很糟糕solution@OlvinRoght... 希望你得到答案。xD@PrashantKumar,再一次-如果某件事有效,并不意味着它是100%正确的。您刚刚在嵌套循环中又添加了一个条件。这意味着,该检查将执行很多次。通常,您必须在循环开始之前检查此条件。您已经完成了这项工作,但消耗了无用的资源。我可以知道为什么吗?我的意思是,执行check
len(json2['selections]]*len(selection['prices]])
多次而不是之前的单一验证不是最好的主意。他想继续循环。使用验证和外部中断不会达到目的。首先,他不想继续循环。Quote:但是,如果eventName不等于某个值,我希望循环继续。使用
break
不是必须的,如果你不想,它不会改变任何东西。您无缘无故地启动无用的循环迭代。