Login Register






Thread Rating:
  • 0 Vote(s) - 0 Average


problem with ranges and lists filter_list
Author
Message
problem with ranges and lists #1
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')
there are 10 types of people in this world,
those who understand binary and those who don't.

skype name: J4W3S.23

Reply

RE: problem with ranges and lists #2
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.
My Blog: http://www.procurity.wordpress.com
Donations: 1HLjiSbnWMpeQU46eUVCrYdbkrtduX7snG

Reply

RE: problem with ranges and lists #3
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.
there are 10 types of people in this world,
those who understand binary and those who don't.

skype name: J4W3S.23

Reply

RE: problem with ranges and lists #4
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:
ArkPhaze
"Object oriented way to get rich? Inheritance"
Getting Started: C/C++ | Common Mistakes
[ Assembly / C++ / .NET / Haskell / J Programmer ]

Reply

RE: problem with ranges and lists #5
(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
Staff will never ever ask you for your personal information.
We know everything about you anyway.

Reply

RE: problem with ranges and lists #6
(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
Staff will never ever ask you for your personal information.
We know everything about you anyway.

Reply

RE: problem with ranges and lists #7
True, combine that logic with my method above: http://www.hackcommunity.com/Thread-Ques...#pid126843

Absolutely NO need for a loop.
ArkPhaze
"Object oriented way to get rich? Inheritance"
Getting Started: C/C++ | Common Mistakes
[ Assembly / C++ / .NET / Haskell / J Programmer ]

Reply

RE: problem with ranges and lists #8
True, combine that logic with my method above: http://www.hackcommunity.com/Thread-Ques...#pid126843

Absolutely NO need for a loop.
ArkPhaze
"Object oriented way to get rich? Inheritance"
Getting Started: C/C++ | Common Mistakes
[ Assembly / C++ / .NET / Haskell / J Programmer ]

Reply

RE: problem with ranges and lists #9
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.
there are 10 types of people in this world,
those who understand binary and those who don't.

skype name: J4W3S.23

Reply







Users browsing this thread: 1 Guest(s)