Sinisterly
problem with ranges and lists - Printable Version

+- Sinisterly (https://sinister.ly)
+-- Forum: Coding (https://sinister.ly/Forum-Coding)
+--- Forum: Python (https://sinister.ly/Forum-Python)
+--- Thread: problem with ranges and lists (/Thread-problem-with-ranges-and-lists)



problem with ranges and lists - J4W3S - 03-22-2013

Hey guys, i started to code a text base game on python but ran in to a problem.
I'm using a list to save different words for directions so the user can type things like east or left to go left.
The line of code that is giving me trouble is.
if direction ==left[range(8)]:
This is the error i get, TypeError: list indices must be integers, not range
Any idea how to fix this?

Here is the rest of the code as well in case that helps.
Code:
left = 'left LEFT Left east EAST East notright port'.split()
right = 'right RIGHT Right west WEST West notleft starboard'.split()
down = 'down DOWN Down notup south SOUTH South back backward backwards'.split()
up = 'up UP Up north NORTH North notdown forward front '.split ()

name = input('what is your name traveller? \n')
print('helo ',name,' how old are you? ')
age = int(input(''))
            
if age >20:
    whatage = 'old'
else:
    whatage = 'young'
print('welcome ',whatage,'adventurer. to the land of coding and games')

direction = input('where would you like to go? \n')
if direction ==left[range(8)]:
    print ('you went left')



RE: problem with ranges and lists - Ex094 - 03-22-2013

EDIT:
Ok so your problem is that in this piece of code:
Code:
if direction ==left[range(8)]:
You are giving a LIST of integers (Range 0 to 8 ) not integers.
So you should try a for loop here as I did:
Code:
left = 'left LEFT Left east EAST East notright port'.split()
right = 'right RIGHT Right west WEST West notleft starboard'.split()
down = 'down DOWN Down notup south SOUTH South back backward backwards'.split()
up = 'up UP Up north NORTH North notdown forward front '.split ()

name = input('what is your name traveller? \n')
print('helo ', name,' how old are you? ')
age = int(input(''))
            
if age >20:
    whatage = 'old'
else:
    whatage = 'young'
print('welcome ',whatage,'adventurer. to the land of coding and games')

direction = input('where would you like to go? \n')
for i in range(len(left)):
    if direction == left[i]:
        print ('you went left')

Explanation:
Code:
for i in range(len(left)):
    if direction == left[i]:
        print ('you went left')
The for loop searches the list ( left[i] ) if it matches the input with one of the values already present in the list, if it does then the output is printed.

Tell me if it solves your problem.


RE: problem with ranges and lists - J4W3S - 03-22-2013

hey, thanks didn't think of that. but i got it to work anyway there's this cool in function i didn't know about it goes like
if direction in left:
thanks for helping though i'm just learning so it's good to know different ways of doing things.
Also just tried you code and it works as well. in case you were wondering.


RE: problem with ranges and lists - ArkPhaze - 03-22-2013

You know you don't even have to loop through the values....
Code:
if direction in left:
    print ('you went left')

Using a loop for this in Python is a waste of time. :whistle:


RE: problem with ranges and lists - 1llusion - 03-23-2013

(03-22-2013, 05:17 AM)Ex094 Wrote: left = 'left LEFT Left east EAST East notright port'.split()
right = 'right RIGHT Right west WEST West notleft starboard'.split()
down = 'down DOWN Down notup south SOUTH South back backward backwards'.split()
up = 'up UP Up north NORTH North notdown forward front '.split ()
[/code]

I'm no Python coder, but I think this will go with every language:

What if the user typed: lEfT or LefT? Your code might not do what it should then. So instead of declaring words with capital letters on different places, simply set the input to lower or to upper.

I'd imagine it would work something like:

(PS: Not actual Python code, just example)
Code:
left = 'left east notright port'.split()
...
...
if direction.toLower == left[i]:
        print ('you went left')

This way you save some space and make the valid input range much bigger Smile


RE: problem with ranges and lists - 1llusion - 03-23-2013

(03-22-2013, 05:17 AM)Ex094 Wrote: left = 'left LEFT Left east EAST East notright port'.split()
right = 'right RIGHT Right west WEST West notleft starboard'.split()
down = 'down DOWN Down notup south SOUTH South back backward backwards'.split()
up = 'up UP Up north NORTH North notdown forward front '.split ()
[/code]

I'm no Python coder, but I think this will go with every language:

What if the user typed: lEfT or LefT? Your code might not do what it should then. So instead of declaring words with capital letters on different places, simply set the input to lower or to upper.

I'd imagine it would work something like:

(PS: Not actual Python code, just example)
Code:
left = 'left east notright port'.split()
...
...
if direction.toLower == left[i]:
        print ('you went left')

This way you save some space and make the valid input range much bigger Smile


RE: problem with ranges and lists - ArkPhaze - 03-24-2013

True, combine that logic with my method above: http://www.hackcommunity.com/Thread-Question-problem-with-ranges-and-lists?pid=126843#pid126843

Absolutely NO need for a loop.


RE: problem with ranges and lists - ArkPhaze - 03-24-2013

True, combine that logic with my method above: http://www.hackcommunity.com/Thread-Question-problem-with-ranges-and-lists?pid=126843#pid126843

Absolutely NO need for a loop.


RE: problem with ranges and lists - J4W3S - 03-27-2013

thanks for the advice, especially the direction.toLower part. and sorry for the late reply i have been without power since Friday because of a snow storm.