首页 > 解决方案 > 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()

标签: python

解决方案


你可以这样做:

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()

推荐阅读