python - Python is_valid = True/False 变量
问题描述
这是我的第一个 SO 帖子!
我正在尝试通过 Murach 的 Python Programming 学习 Python——顺便说一句,这是一个很好的资源......
关于以下代码:
is_valid = True
customer_type = input('Enter customer type (r/w): ')
if customer_type == 'r' or customer_type == 'w':
print(f'You entered: {customer_type}')
# pass
else:
print('Customer type must be "r" or "w".')
is_valid = False
我不明白为什么需要有效的 True & False 变量,因为当我将它们都注释掉时,代码似乎可以正常工作?
提前致谢!!
解决方案
我认为代码应该是这样的:
is_valid = True
customer_type = input('Enter customer type (r/w): ')
if customer_type == 'r' or customer_type == 'w':
print(f'You entered: {customer_type}')
# pass
else:
print('Customer type must be "r" or "w".')
is_valid = False # space here
现在is_valid
值将显示您是否输入了有效输入。在此示例中,它不执行任何操作。我认为本书后面可能还有与此变量相关的其他代码。
推荐阅读
- javascript - 关闭nodejs程序内的Oracle DB连接后如何从函数返回值
- shell - 无法删除大于 2GB 的文件
- php - 是否可以使用 php、jquery、mysql 以一种形式保存多个文件上传和其他数据?
- apache-spark - 如何删除数据集列名称中的特殊字符
- r - “最多”的正则表达式
- jestjs - 开玩笑测试不会通过,即使第一个测试被评论
- python - 如何对分段/集群的距离大于 1 天的时间序列序列进行分段/聚类?
- html - 为什么这段代码在 Chrome 和 Firefox 中的行为不同/我该如何解决?
- php - WordPress “wp_register_style 调用不正确”?
- python - 从txt文件中提取文本并将其转换为df