Reputation: 43
I'm trying to make a board game that is essentially a word searcher. I would like to create a board that would have labels like this:
1 2 3 4
1 q w e r
2 p o t t
3 q y h f
I have already created a working code that would allow the user to input the number of rows and columns as well as the contents of the board itself. The problem is now that I can't seem to print it properly.Here is my working code:
# -*- coding: utf-8 -*-
"""
Created on Tue Sep 17 00:40:13 2019
@author: Oscar Simon Velasco
"""
def get_words(n,words):
global given_words
count=1
final=[given_words]
while n!=count:
count=count+1
words=input('Enter a word:')
final.append(words)
return final
def sort_words(sorted__):
global sorted_words
bank={}
x=sorted_words
x.sort()
W2=1
print('Sorted words:')
for y in x:
print(y)
for z in sorted_words:
bank.update({W2:z})
W2=W2+1
print(bank)
def populate_board(RxC,letter):
global letters_in_rows
count=1
final=[letters_in_rows]
while RxC!=count:
letter=input('Enter row:')
count=count+1
final.append(letter)
print(final)
return final
def print_board(a,row):
b=[]
for x in a:
b.append(x)
Labels_columns=range(1,row+1)
Labels_rows=range(1,row+1)
print(" ", end="")
for c in Labels_columns :
print(" " + str(c), end="")
print()
for i in range(1,row+1) :
print(str(Labels_rows[i]), end="")
for elt in b[i] :
print(" " + str(elt), end="")
print()
num_words=int(input('Enter desired number of words:'))
given_words=input('Enter a word:')
sorted_words=get_words(num_words,given_words)
sort_words(sorted_words)
Number_rows=int(input('Enter number of rows and column:'))
letters_in_rows=str(input('Enter row:'))
board=populate_board(Number_rows,letters_in_rows)
print_board(letters_in_rows,Number_rows)
The function that supposedly prints the board is:
def print_board(a,row):
b=[]
for x in a:
b.append(x)
Labels_columns=range(1,row+1)
Labels_rows=range(1,row+1)
print(" ", end="")
for c in Labels_columns :
print(" " + str(c), end="")
print()
for i in range(1,row+1) :
print(str(Labels_rows[i]), end="")
for elt in b[i] :
print(" " + str(elt), end="")
print()
But I get the error 'IndexError: list index out of range' on line:
print(str(Labels_rows[i]), end="")
I can't understand why its doing it, knowingly I am still a newbie in python I am sure there must be an underlying cause here. I really need help on how to address this problem. THANK YOU IN ADVANCE!
Upvotes: 0
Views: 47
Reputation: 10809
When you do this:
Labels_rows=range(1,row+1)
You don't have a list (just an FYI). You're binding a Range
object to the variable name Labels_rows
. Let's say row
is 5. If you evaluate the contents, they look like this:
row = 5
Labels_rows = range(1, row+1)
print(*Labels_rows)
Output:
1 2 3 4 5
As expected.
In your for loop, you iterate over an identical range, but you use the values in that range as indecies for your Labels_rows
range object. This means you will start iterating at index 1 (instead of 0, which does not result in an error, but is probably not deliberate), and your loop will try to run to index 5 (which would be the sixth element in your Labels_rows
range object, which doesn't exist, hence the error).
Upvotes: 0
Reputation: 2189
Just change it to range(1, row)
. row + 1
exceeds the length of the list.
Upvotes: 0