python - python while循环输入由分隔符分隔的2个整数
问题描述
我需要能够接受条目为 3,4 ,基本上是用逗号分隔的 2 个整数。我需要一个while循环来继续要求输入,直到他们以正确的格式输入2个数字。这是我到目前为止的代码
def main():
try:
move = [int(s) for s in input("Select a cell (row,col) > ").split(",")]
except:
while move != []#stuck here
x = move[0]
y = move[1]
main()
解决方案
你可以这样做:
def main():
print('Enter the two points as comma seperated, e.g. 3,4')
while True:
try:
x, y = map(int, input().split(','))
except ValueError:
print('Enter the two points as comma seperated, e.g. 3,4')
continue
else:
break
main()
推荐阅读
- javascript - 使用 Chrome 扩展和 Selenium Webdriver 设置 cookie
- python - Python/pandas 数据帧中的数值模糊比较连接
- ruby-on-rails - Rails collection_select,使用数据库数据和任意值填充下拉字段
- css - 自动处理 CSS 文件
- python - cx_freeze 调试控制台?
- dax - 根据 YTD Sum 过滤 Dax 计算
- php - 一个mysqli连接用于多个数据库功能
- list - 嵌套列表的最后一个最大值?
- c# - 从基类向下投射
- c - 如何在包含句点的同时将较长字符串中的句子复制到新数组中?