python - slice/stride by variable? -
background
i'm attempting code basic letter game in python. in game, computer moderator picks word out of list of possible words. each player (computer ai , human) shown series of blanks, 1 each letter of word. each player guesses letter , position, , told 1 of following:
- that letter belongs in position (the best outcome)
- that letter in word, not in position
- that letter not in of remaining blank spaces
when word has been revealed, player guess letters correctly wins point. computer moderator picks word , starts again. first player 5 points wins game. in basic game, both players share same set of blanks they're filling in, players benefit each other's work.
my question
is there way use variable containing integer designate degree of slice/stride?
because secret word randomized, have no way of knowing how many characters contain. want check user's input (guess) first based on accuracy in regards place value user selected, before checking see if letter appears in secret word @ (not exclusively in place specified). i'm thinking maybe use len() determine number of places in secret word, use player's numerical input slice/stride on correct character place in word specified player.
how user-supplied integer slice/stride command? doesn't seem accept assigning input variable , placing variable within slice/stride.
i'm beginner python apologies if dumb question--my initial research didn't turn anything.
simply put, yes, can this. you'd want ensure input you're getting is int
, , work there.
some_word = "this song doesn't end" stride = int(raw_input("stride distance? ")) print some_word[::stride] # prints out "ti stesn htdented"
after looking in comments, can isolate single character in string/sequence indexing it.
some_word = "this song doesn't end" iso = int(raw_input("isolated character? ")) print some_word[iso] # prints out whatever character in position, 0-based.
Comments
Post a Comment