TypeError: list indices must be integers or slices, not tuple (Python 3.11) - python

I am trying to create a noughts and crosses game in python, and I got an error in the bold text area in checkGridRow(). This is where I want to check if the game has been won by any player by checking for "XXX" or "OOO" in a horizontal row. At the end of the code, I use the parameter of "position" as the Y position in the grid and so pass 0, 1 and 2 to check all the rows. However I have run into the error in the title, and I don't know what it means despite searching, as I have no tuples in my code (as far as I can see). I am a beginner so please try to explain in relatively simple terms, thank you for helping
grid = [["_","_","_"],["_", "_", "_"],["_", "_", "_"]]
game = True
def checkGridRow(position):
n = 0
***if grid[position,n]!= "_":***
if grid[position,n]== grid[position,n+1] and grid[position,n+1]==grid[position,n+2]:
game = False
return game
def checkGridCol():
tempList = ""
c1 = [grid[0,0], grid[1,1], grid[2,2]]
c2 = [grid[2,0], grid[1,1], grid[0,2]]
if not any("_" in i for i in c1):
for var in c1:
tempList+= var
if tempList == "XXX":
game = False
elif tempList == "OOO":
game = False
return game
def PlayerTurnX():
column = int(input("enter column >> 1,2,3: "))
column = column -1
while str(column+1) not in "123":
column = int(input("enter column 1,2,3: "))
column = column-1
row = int(input("enter row >> 1,2,3: "))
row = row-1
while str(row+1) not in "123":
row = int(input("enter row >> 1,2,3: "))
row= row-1
if grid[row][column]=="_":
grid[row][column] = "X"
elif grid[row][column]!= "_":
print("Space taken")
row = int(input("enter row >> 1,2,3: "))
row = row-1
for item in grid:
print(item[0]+" "+item[1]+" "+item[2])
def PlayerTurnO():
column = int(input("enter column: >> 1,2,3: "))
column = column-1
while str(column+1) not in "123":
column = int(input("enter column >> 1,2,3: "))
row = int(input("enter row: >> 1,2,3: "))
row = row-1
while str(row+1) not in "123":
row = int(input("enter row: >> 1,2,3: "))
row = row-1
if grid[row][column]=="_":
grid[row][column] = "O"
else:
print("Space taken")
column = int(input("enter column>> 1,2,3: "))
column = column-1
n=n-1
for item in grid:
print(item[0]+" "+item[1]+" "+item[2])
while game:
print("Player X, your turn!")
PlayerTurnX()
checkGridRow(0)
checkGridRow(1)
checkGridRow(2)
checkGridCol()
print("")
print("")
print("Player O, your turn!")
PlayerTurnO()
checkGridRow(0)
checkGridRow(1)
checkGridRow(2)
checkGridCol()
I've tried searching the error message and still cannot figure out where the tuple is, as far as I know tuples look like this myTuple = (x, y, z)

grid[position,n] is not the correct syntax for accessing a nested list.
Use grid[position][n] instead.

Related

How do I get the correct outputs for this chess board?

I am trying to make a program of a chess board, When a user inputs an x and y value it will either output "black" or "white".
x = int(input("Please enter your (x) first number 1-8::"))
y = int(input("Please enter your (y) second number 1-8::"))
column = x % 2
row = y % 2
if column %2 == 0 and row %2 == 1:
print("")
print("white")
elif row %2 ==0 and column %2 == 1:
print("")
print("black")
Whenever i input 1 for "x" and 2 for "y" it outputs "black", great this is the correct output. But whenever i input some other numbers such as 2 and 2, it gives me a blank output. Whenever i input 1 and 4, it outputs "black" which the correct output should have been "white. How do i make it so that whenever user inputs two numbers ranging from 1 to 8, it outputs the correct colour tile? I am not trying to make the code more advanced but would appreciate some help!
This is the chess board i am basing the colours on.( Do not mind the text on the picture)
Instead of looking at x and y separately, just check the sum.
If the sum is even, it's black, if the sum is odd, it is white.
I added a lookup of the name in a python dict, but you can just do it with if conditions if you prefer.
x = int(input("Please enter your (x) first number 1-8::"))
y = int(input("Please enter your (y) second number 1-8::"))
color_picker = {0: "Black", 1: "White"}
if not 0<x<9 or not 0<y<9:
print("Input valid number!!")
else:
color
print(color_picker[(x+y)%2])
Let me know if it helps.
if column %2 == 0 and row %2 == 1:
...
elif row %2 ==0 and column %2 == 1:
...
This covers the case where column is even and row is odd, and the case where row is even and column is odd.
But what about the cases where they are both even, or both odd?
x = int(input("Please enter your (x) first number 1-8::"))
y = int(input("Please enter your (y) second number 1-8::"))
column = x
row = y
if (column + row) %2 == 0:
print("")
print("black")
elif (column + row) %2 == 1:
print("")
print("white")
else:
print("Input valid number!!")

Noughts And Crosses game loop iterating too many times

This is my noughts and crosses code i am still early on in making it and I am trying to do it as independently as possible without too much help from google- this is a simple question but why is it that when you get 3 in a row within the 2d list that the loop I have made continues to iterate one last time before ending? thanks a lot
won = False
sum = 0
for i in range (3):
if grid[i][i] == "X":
sum +=1
elif grid[i][i] == "O":
sum -=1
if sum == 3 or sum == -3:
won = True
sum = 0
for i in range (3):
if grid[row][i] == "X":
sum +=1
elif grid[row][i] == "O":
sum -=1
if sum == 3 or sum == -3:
won = True
sum = 0
for i in range (3):
if grid[i][column] == "X":
sum +=1
elif grid[i][column] == "O":
sum -=1
if sum == 3 or sum == -3:
won = True
return won
#############################main program#############################
grid = [["-","-","-"],
["-","-","-"],
["-","-","-"]]
for x in grid:
print (x)
win = False
while win == False:
print("\nCrosses\n")
column = int(input("Enter a Column\n"))
row = int(input("Enter a Row\n"))
grid[row][column] = ("X")
for x in grid: print (x)
win = checkwin(grid, row, column)
print("\nNoughts\n")
column = int(input("Enter a Column\n"))
row = int(input("Enter a Row\n"))
grid[row][column] = ("O")
for x in grid: print (x)
win = checkwin(grid, row, column)```
After your check whether last placed X made the player win, you don't exit the loop in any case (the while loop condition is only checked when an iteration starts, not all the time) - the program always continues and allows O player to move, even if win is already true. To change that behaviour, you need to break from the loop if the X player won, using break keyword. Your loop would look like this:
while win == False:
print("\nCrosses\n")
column = int(input("Enter a Column\n"))
row = int(input("Enter a Row\n"))
grid[row][column] = ("X")
for x in grid: print (x)
win = checkwin(grid, row, column)
if win:
break
print("\nNoughts\n")
column = int(input("Enter a Column\n"))
row = int(input("Enter a Row\n"))
grid[row][column] = ("O")
for x in grid: print (x)
win = checkwin(grid, row, column)

Error in repeating input in a tictactoe game

I am trying to prevent the user from inputting a square that is already marked, but the for loop moves on to the next player's input without decrementing the value of i by one, so the player 1 can repeat his input. How do I fix this?
arr = [[0,0,0],[0,0,0],[0,0,0]]
grid = grid(arr)
grid.print_grid()
for i in range(9):
row = int(input("Enter the row name: "))
col = int(input("Enter the column name: "))
if(arr[row][col] == 0):
if(i%2):
arr[row][col] = 1
else:
arr[row][col] = 2
else:
print("\nThat square has already been marked! Please select another square")
i = i-1
continue
grid.print_grid()
res = grid.grid_checker()
if (res == 1):
print("\nPlayer 1 wins the game!")
break
elif(res == 2):
print("\nPlayer 2 wins the game!")
break
elif(i == 8):
print("\nThe game has ended in a draw!")
You need to store another variable to keep track of whose turn it is. You cannot modify the variable you are looping on while you are in the loop body. This means that i cannot be manipulated while you are running in the loop. Here's how I would change it.
turn = 0
while True:
row = int(input("Enter the row name: "))
col = int(input("Enter the column name: "))
if(arr[row][col] == 0):
if(i%2):
arr[row][col] = 1
turn = turn + 1
else:
arr[row][col] = 2
turn = turn + 1
else:
print("\nThat square has already been marked! Please select another square")
continue
grid.print_grid()
res = grid.grid_checker()
if (res == 1):
print("\nPlayer 1 wins the game!")
break
elif(res == 2):
print("\nPlayer 2 wins the game!")
break
elif(turn == 8):
print("\nThe game has ended in a draw!")
Here we're saving the turn number in the variable turn and only incrementing the variable when we can confirm a player has successfully completed their turn.
Why you cannot modify i: For optimisations, loops are often expanded by python before they are converted to assembly instructions. For example a simple loop like this:
for i in range(9):
print(i)
May be expanded to something like this:
i = 0
print(i)
i = 1
print(i)
# and so on, upto
i = 9
print(i)
This is done to avoid having to jump around in memory. So as you can see here, i is reassigned for every iteration of the loop. Therefore, even if you change the value of i in the body of the loop, it will simply be reassigned before the next iteration.

PYTHON Problem: Why is my code showing "NONE" when line no. 9 is executed? can anyone assist me?

print("\tWELCOME TO DRY_RUN CLASS ASSAIGNTMENT!\t")
userList = []
def mainSystem():
number = 1
userInput = int(input("Enter the size of the List: "))
if userInput > 0:
for x in range(0, userInput):
variable = int(input(print("Enter number",number )))
number = number + 1
userList.append(variable)
else:
print("Number should not be less than or equal to '0'!")
def maxAll():
maxofall = 0
for element in userList:
if element > maxofall:
maxofall = element
print("The maximum number is:", element)
while True:
mainSystem()
askUser = int(input("What do you want to do with the numbers?\n1.Max All\n2.Average\n3.Quit\nYour answer: "))
if askUser == 1:
maxAll()
this is the code i am using right now...
what do i need to fix i am getting an error like this wheni am executing line no. 9
:-
Enter Number whatever the number is
Noneinput starts here
???
def mainSystem():
number = 1
userInput = int(input("Enter the size of the List: "))
if userInput > 0:
for x in range(0, userInput):
variable = int(input("Enter number {} : ".format(number) ))
number = number + 1
userList.append(variable)
else:
print("Number should not be less than or equal to '0'!")
Change your function to this.
print() is a function. Which returns nothing (None) in your case.
input() function thinks this None object is worth displaying on console.
Hence None appears on screen before taking any input.
There are a couple of problems:
1)You have a misplaced print() inside input() in your mainSystem() function
2)You probably want to use f-strings to print the right number instead of the string literal 'number'
def mainSystem():
number = 1
userInput = int(input("Enter the size of the List: "))
if userInput > 0:
for x in range(0, userInput):
variable = int(input(f"Enter number {number} : "))
number = number + 1
userList.append(variable)

Computer guessing a user-selected number within a defined range

This is my code for a game in which the computer must guess a user defined number within a given range. This is a challenge from a beginners course/ book.
I'd like to draw your attention to the 'computerGuess()' function. I think there must be a more eloquent way to achieve the same result? What I have looks to me like a botch job!
The purpose of the function is to return the middle item in the list (hence middle number in the range of numbers which the computer chooses from). The 0.5 in the 'index' variable equation I added because otherwise the conversion from float-int occurs, the number would round down.
Thanks.
Code:
# Computer Number Guesser
# By Dave
# The user must pick a number (1-100) and the computer attempts to guess
# it in as few attempts as possible
print("Welcome to the guessing game, where I, the computer, must guess your\
number!\n")
print("You must select a number between 1 and 100.")
number = 0
while number not in range(1, 101):
number = int(input("\nChoose your number: "))
computerNumber = 0
computerGuesses = 0
higherOrLower = ""
lowerNumber = 1
higherNumber = 101
def computerGuess(lowerNumber, higherNumber):
numberList = []
for i in range(lowerNumber, higherNumber):
numberList.append(i)
index = int((len(numberList)/2 + 0.5) -1)
middleValue = numberList[index]
return middleValue
while higherOrLower != "c":
if computerGuesses == 0:
computerNumber = computerGuess(lowerNumber, higherNumber)
elif higherOrLower == "l":
higherNumber = computerNumber
computerNumber = computerGuess(lowerNumber, higherNumber)
elif higherOrLower == "h":
lowerNumber = computerNumber + 1
computerNumber = computerGuess(lowerNumber, higherNumber)
print("\nThankyou. My guess is {}.".format(computerNumber))
computerGuesses += 1
higherOrLower = input("\nHow did I do? If this is correct, enter\
'c'. If your number is higher, enter 'h'. If it is lower, enter 'l': ")
print("\nHaha! I got it in {} attempt(s)! How great am I?".format\
(computerGuesses))
input("\n\nPress the enter key to exit.")
Like this ?
import math
def computerGuess(lowerNumber, higherNumber):
return int((lowerNumber+higherNumber)/2)

Categories