Python 2.7 try and except ValueError

让人想犯罪 __ 提交于 2021-02-17 21:25:29

问题


I query user input which is expected to be an int by using int(raw_input(...))

However when the user doesn't enter an integer, i.e. just hits return, I get a ValueError.

def inputValue(inputMatrix, rangeRows, rangeCols, defaultValue, playerValue):
    rowPos = int(raw_input("Please enter the row, 0 indexed."))
    colPos = int(raw_input("Please enter the column, 0 indexed."))
    while True:
        #Test if valid row col position and position does not have default value
        if rangeRows.count(rowPos) == 1 and rangeCols.count(colPos) == 1 and inputMatrix[rowPos][colPos] == defaultValue:
            inputMatrix[rowPos][colPos] = playerValue
            break
        else:
            print "Either the RowCol Position doesn't exist or it is already filled in."
            rowPos = int(raw_input("Please enter the row, 0 indexed."))
            colPos = int(raw_input("Please enter the column, 0 indexed."))
    return inputMatrix

I tried to be smart and use try and except to catch the ValueError, print a warning to the user and then call the inputValue() again. Then it works when the user enters return to the query but falls over when the user correctly then enters an integer

Below is the part of the amended code with the try and except:

def inputValue(inputMatrix, rangeRows, rangeCols, defaultValue, playerValue):
    try:
        rowPos = int(raw_input("Please enter the row, 0 indexed."))
    except ValueError:
        print "Please enter a valid input."
        inputValue(inputMatrix, rangeRows, rangeCols, defaultValue, playerValue)   
    try:
        colPos = int(raw_input("Please enter the column, 0 indexed."))
    except ValueError:
        print "Please enter a valid input."
        inputValue(inputMatrix, rangeRows, rangeCols, defaultValue, playerValue)   

回答1:


A quick and dirty solution is:

parsed = False
while not parsed:
    try:
        x = int(raw_input('Enter the value:'))
        parsed = True # we only get here if the previous line didn't throw an exception
    except ValueError:
        print 'Invalid value!'

This will keep prompting the user for input until parsed is True which will only happen if there was no exception.




回答2:


Instead of calling inputValue recursively, you need to replace raw_input with your own function with validation and retry. Something like this:

def user_int(msg):
  try:
    return int(raw_input(msg))
  except ValueError:
    return user_int("Entered value is invalid, please try again")



回答3:


Is something like this what you're going for?

def inputValue(inputMatrix, defaultValue, playerValue):
    while True:
        try:
            rowPos = int(raw_input("Please enter the row, 0 indexed."))
            colPos = int(raw_input("Please enter the column, 0 indexed."))
        except ValueError:
            continue
        if inputMatrix[rowPos][colPos] == defaultValue:
            inputMatrix[rowPos][colPos] = playerValue
            break
    return inputMatrix

print inputValue([[0,0,0], [0,0,0], [0,0,0]], 0, 1)

You were right to try and handle the exception, but you don't seem to understand how functions work... Calling inputValue from within inputValue is called recursion, and it's probably not what you want here.



来源:https://stackoverflow.com/questions/5025399/python-2-7-try-and-except-valueerror

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!