简体   繁体   中英

ValueError: invalid literal for int() with base 10:

This is a game that asks users to enter a location on a 3 by 3 grid and then another user attempts to guess the location. Having a problem debugging an error.

What is the problem with this line? Col=int(guess[1:])-1

In the following code?

It returns the following error when I try and run it in repl.it.

Traceback (most recent call last): File "python", line 84, in File "python", line 71, in init ValueError: invalid literal for int() with base 10: ''

#Creation of the grid and the user interface list
grid = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
turns=0
guess="x"
#Function to clear screen
def cls(): 
  print ("\n" * 100)

#Function to validate coordinate
def validate_entry():
  valid=False
  while valid==False:
    guess=input("Enter a location from A1 to C3: ")
    loc = guess.upper() #Use upper case
    row = guess[0:1]
    col = guess[1:2]
    if(row!="A" and row!="B" and row!="C") or (col!="1" and col!="2" and col!="3"):
      print("Your location is invalid.")
      print("")
      return False #not actually needed, but helps see what is happening
    else:
     valid=True
     return True
#Function to set the location of the treasure
def settingTreasure(grid):

    treasureSet=input("Set location by entering a location from A1 to C3: ")
    #Converting the letter into a coordinate in the list
    treasure_letter=str(treasureSet[:1])
    if treasure_letter in ["a", "A"]:
      locRow=0
    elif treasure_letter in ["b", "B"]:
      locRow=1
    elif treasure_letter in ["c", "C"]:
      locRow=2
    #Converting the number into a list coordinate
    locCol=int(treasureSet[1:])-1
    grid[locRow][locCol]=1

settingTreasure(grid)
cls()
#Displaying the user interface in a clean manner
def gameBoard():
  UI = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
  for i in range(0,len(UI)):
    print(UI[i])

gameBoard()

#The main loop that contains the game
def _init_(turns, guess):
  turns=turns+1
 #If the player exceeds three turns they lose
  if turns>3:
    print("Too many turns! You lose!")
    quit()

  #Prints number of turns
  print(" ")
  print("Turn " + str(turns))
  validate_entry()
  #Converting the letter into a coordinate in the list
  guess_letter=str(guess[:1])
  if guess_letter in ["a", "A"]:
    Row=0
  elif guess_letter in ["b", "B"]:
    Row=1
  elif guess_letter in ["c", "C"]:
    Row=2
  #Converting the number into a list coordinate
  Col=int(guess[1:])-1

  #Test to see if they guessed treasure location
  if grid[Row][Col]==1:
    print(" ")
    print("found treasure!")
    quit()
  else:
    print(" ")
    print("Treasure not here!")

#Loop for the main game
while True:
 _init_(turns, guess)

After validating the user's input you didn't return the value to be used later on, so the guess value remains as "x" as when the code was initialized

Try changing the validate_entry function and the init function

def validate_entry():
  valid=False
  while valid==False:
    guess=input("Enter a location from A1 to C3: ")
    loc = guess.upper() #Use upper case
    row = guess[0:1]
    col = guess[1:2]
    if(row!="A" and row!="B" and row!="C") or (col!="1" and col!="2" and col!="3"):
      print("Your location is invalid.")
      print("")
      return False #not actually needed, but helps see what is happening
    else:
     valid=True
     return guess


def _init_(turns):
  turns=turns+1
 #If the player exceeds three turns they lose
  if turns>3:
    print("Too many turns! You lose!")
    quit()

  #Prints number of turns
  print(" ")
  print("Turn " + str(turns))
  guess = validate_entry()
  #Converting the letter into a coordinate in the list
  guess_letter=str(guess[:1])
  if guess_letter in ["a", "A"]:
    Row=0
  elif guess_letter in ["b", "B"]:
    Row=1
  elif guess_letter in ["c", "C"]:
    Row=2
  #Converting the number into a list coordinate
  Col=int(guess[1:])-1

  #Test to see if they guessed treasure location
  if grid[Row][Col]==1:
    print(" ")
    print("found treasure!")
    quit()
  else:
    print(" ")
    print("Treasure not here!")

_init_(turns)

The validate_entry function now returns the guess value, which is then passed on to the init function inside the function call

Side note: you can use guess_letter.upper() == "A" instead of guess_letter in ["a", "A"]. The upper() helps to convert the letter to uppercase. But you might want to check if guess_letter.isalpha() before that. I think this way might be easier because you just need to change one character after that. Another way is to get the index of the letter in string.ascii_uppercase after testing that it is an alpha

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM