python - Python 异常无效语法
问题描述
Pythonexcept
不工作。我正在努力
r = requests.get('URL') # URL returns something like: {"code": ["12345678"]}
print(r.text)
parse = json.loads(r.text)
getcode = False
while not getcode:
time.sleep(2)
codeparse = parse["code"]
print("Unable to get SMS Code, sleeping for 2 seconds...")
except KeyError:
pass
getcode = parse["code"]
我已经尝试了我所知道的一切。有什么我需要导入或缺少的东西吗?
编辑:更新以根据要求添加更多代码。
解决方案
这只是无效的语法:您不能将except
块作为while
循环的一部分:
while not getcode:
...
except KeyError:
pass
REPL 中的示例:
>>> while 1:
... print("g")
... except KeyError:
File "<stdin>", line 3
except KeyError:
^
SyntaxError: invalid syntax
>>>
SyntaxError
因此,无论循环内有什么,您都会立即得到一个。
正确的语法是:
try:
# some code
...
except KeyError:
...
推荐阅读
- c# - 如果子元素的一个元素等于一个值,如何检索父元素中的属性值?
- javascript - 无法在 React 的映射函数中呈现返回语句
- asp.net-mvc - 我的无服务器 Web 应用程序没有 HOME 控制器,如何在我的剃须刀页面上传递参数和执行函数
- python - “rpm -qf”的等价物是什么
" 使用 Python rpm 库? - azure - 在 Azure 中,是否可以创建补救策略以启用存储帐户中的诊断设置日志记录?
- sql - 有没有办法将 2 个数据库置于调试和发布配置中,并在 2 个数据库之间切换以进行调试和发布模式?
- angular - 以 Angular 8 引用 Angular 材料为例,将组件注入到组件中
- javascript - Sails.js 测试中的超时问题
- flutter - 当用户单击平面按钮时如何在颤动中显示奖励视频
- java - Tomcat 7.0 版本上的 spring boot 2.2.1 Web 应用程序部署问题