Integers as the only valid inputs

前端 未结 3 649
暗喜
暗喜 2021-01-27 10:22

What I\'m trying to do is ask users for two inputs, if one of the inputs is less than zero or if the input is some string then ask for the inputs again. The only valid input are

3条回答
  •  半阙折子戏
    2021-01-27 10:58

    Use the try except method

    while stringval = input("What is the number you want to enter"):
      try:
        intval = int(stringval)
        if intval > -1:
          break
      except ValueError:
        print 'Invalid answer, try again'
    
    # Process intval now
    

提交回复
热议问题