Reputation: 89
I have this to get input and put it in a list:
def start():
move_order=[raw_input("Enter your moves: ").split()]
And I only want the characters A, D, S, C, H (it's for a game >_>) to be allowed. I've tried using the regular expressions stuff:
if re.match('[ADSCH]+', [move_order]) is False:
print "That's not a proper move!"
return start()
...in different forms...
string_test=re.compile('[ADSCH]+')
if string_test.match(move_order]) is False:
print "That's not a proper move!"
return start()
Aaaaand can't get it to work. I'm definitely doing something wrong within those code blocks, I tried to figure it out, but it's not working. Learning what I'm doing wrong would be nice, but the solution to my problem would teach me a lot more I feel like. I might not even need to use re, but it seemed to me that that was a space efficient way to accomplish what I want. My immediate problem I think is that I don't know how to get the re to use lists (unless (certainly) there's other glaring problems trained eyes can find).
I'll go ahead and ask since I'll probably screw this up too, but I also need to have it make sure that a C is never after an H... but a tiny hint for that would be acceptable because I like figuring things out.
Upvotes: 1
Views: 118
Reputation: 2499
With such a small range you could just iterate the move_order and check if each element exists in the allowed moves
def start():
move_order=[c for c in raw_input("Enter your moves: ")]
moves = ['A','D','S','C','H']
for c in move_order:
if c not in moves:
print "That's not a proper move!"
return start()
Edit: Solution taking into account suggestions in comments
def start():
move_order=list(input("Enter your moves: "))
while set(move_order) - set('ADSCH'):
for x in set(move_order) - set('ADSCH'):
move_order = [input("%s is not a vaild move, please enter another move" % x) if i==x else i for i in move_order]
Print "Player ready" #Rest of program..
If I understand your question, i don't think split is doing what you think it is. It is not splitting each character of the string entered by the user into an array.
Upvotes: 0
Reputation: 29033
What are you doing with the characters afterwards? There might not be any need to even have this step. Design it so that when you're doing the actions for the moves, you don't/can't do any invalid ones.
def move_left():
print "Moving left"
def move_down():
print "moving down"
#...etc
def invalid_move():
print "invalid move"
# This dictionary connects move command letters
# with the functions above that do the moving
move_funcs = {
'A': move_left,
'S': move_down,
'D': move_right,
'C': wtf_keyboard_layout,
'H': do_H_thing
}
moves = raw_input("Enter your moves: ")
for move in moves.upper():
# this gets the right function for the move,
# e.g. A gets move_left
# but any character not there gets invalid_move
move_func = move_funcs.get(move, invalid_move)
move_func()
Upvotes: 0
Reputation: 1925
There're many ways to match 'ADSCH'
You can use raw_input().upper()
to get rid of 'adsch'
use re
: don't do split before
def start():
movement = raw_input("Enter your moves: ").upper()
if re.match('^[ADSCH\s]*$', movement):
# it's a legal input
use str.strip
if movement.strip(' ADSCH') == '':
# it's a legal input
use all
with move_order
list(works with string either):
def start():
move_order=[raw_input("Enter your moves: ").upper().split()]
if all((x in 'ADSCH' for x in move_order)):
# it's a legal input
use any
with move_order
list(works with string either):
if any((x not in 'ADSCH' for x in move_order)):
# it's an illegal input
Upvotes: 1
Reputation:
I don't know python but you could do something like this:
for c in move_order:
if (c == 'A' or c == 'D' c == 'S' or c == 'C' or c == 'H'):
[do something with the character]
Upvotes: 0