Is it possible to disable negative indexing? [closed]

2024/11/16 13:30:13

So i am developing the game connect 4 and i need to rid of negative indexing because it causes the game to act funny. Basically, the column a player accesses is based on a group of list combined into one single list to form an array. e.g

    grid1 = ['A','B','C','1']grid2 = ['D','E','F','2']grid3 = ['G','H','I','3']grid4 = ['J','K','L','4']# Now if we combine all three lists, we getTotal_Grid = [['A','B','C','1']['D','E','F','2']['G','H','I','3']['J','K','L','4']]# We have a total of 4 columns and 4 rows in this grid# Here is the format of how we access values in list Total_Grid[row][col]

So to access letter 'G', we do Total_Grid[2][0]. because 'G' is in row 2, column 0. Drawing out the actual grid, we have:

    |  |  |  |  |-------------|  |  |  |  |-------------|  |  |  |  |-------------|  |  |  |  |-------------# As you can see, the grid is 4x4

Now because in connect 4, you don't get to choose what row the counter goes in, (it usually drops to the bottom of the grid), we will designate a value for row.

    row = 3# Lets ask the user for inputcol = input("What column would you like to drop your counter in? ")# let's say user inputs 3, the counter will drop to [3][3] in the gridcol = 3|   |   |   |   |-----------------|   |   |   |   |-----------------|   |   |   |   |-----------------|   |   |   | X |-----------------        

My problem now arises because for instance, if the user enters a negative number for the column value, it still works because it indexes backward but i want to disable this because it messes up the game when the AI tries to block the player from connecting 4 dots

Answer

You might encapsulate your check and print functions into one callable function:

def print_only_if_non_negative(x):if x >= 0:print(x)for i in range(5):print_only_if_non_negative(i-5)
https://en.xdnf.cn/q/120323.html

Related Q&A

Google Colab Notebook completely freezes when training a YOLO model

I am following a tutorial to train custom object detection model using YOLO. This is the tutorial and also where I got the Notebook Everything works fine until the training bit which is the last cell. …

Generate 4 columns of data such that each row sum to 100

How do I write a python program that can randomly generate 4 columns of data such that the sum of the numbers of each row is 100?

Nan to Num Python

I have multiple array that for those I calculate a linear regression, but sometimes it gives me 0/0 values which gives me a NaN. I know that to convert an array where there are numbers that are NaN you…

Class constructor able to init with an instance of the same class object

Can python create a class that can be initialised with an instance of the same class object?Ive tried this:class Class(): def __init__(self,**kwargs):print selfself = kwargs.get(obj,self)print selfif …

Python Logical Operators

I am currently learning Python3, but I dont understand Logical Operators. Here is the link: http://pymbook.readthedocs.org/en/py3/operatorsexpressions.html#logical-operators First of all, it seems that…

Partial Pivoting In Pandas SQL Or Spark

Partial Pivoting In Pandas SQL Or Spark Make the Year remain as Rows, and have the States Transpose to columns Take Pecentage value of a Gender Male Race White, InputOutput

Python to create a find-replace dictionary from a word vba macro

I have a very big macro Selection.Find.ClearFormattingSelection.Find.Replacement.ClearFormattingWith Selection.Find.Text = "asa".Replacement.Text = "fsa".Forward = True.Wrap = wdFin…

Converting many .txt files into csv and combining them

I have many .txt files. I want to convert a few files ending with specific names into csv and combine them into one csv. ### Folder Name: text_files python_gramm.py aadd01.txt aaxx02.txt aaff03.txt hhd…

Calculation between two columns in Python?

When I tried to do some calculation between two columns (like division), I get an error: column_ratio[x]=(float(column1[y]))/(float(column2[z])) TypeError: tuple indices must be integers, not str. C…

Why does input() always return a string?

Here is my code:age = input("How old are you?: ") print (age) print (type(age))Result:How old are you?: 3535class str <<--- This is problem!But, If I use.. age = int(input("How …