python 2.7 - using .find for multiple letters in a string -


i'm working on hobby project. i'm attempting make hangman game in python. far works nice. there's 1 problem. if type letter appears in word 2 times, can't second letter appear. i've been toying around string.find , string.count methods no avail. have idea how go doing this? i'm stumped.

#!bin/bash/python  import os import time  word = 'megalopolis' l = len(word) list = [] n=0 while n!=l:     list.append('-')     n+=1 os.system('cls' if os.name=='nt' else 'clear') print list  i=3  while i!=0:     x = raw_input('enter letter: ')     if x in word , x!='':         print 'good choice!'         count=word.count(x)         loc=word.find(x)         print count          print loc          list[loc]=x         os.system('cls' if os.name=='nt' else 'clear')         if '-' not in list:             break         print list     else:         print 'sorry...'         i-=1         if i==2:             print 'you have '+`i`+' more chances.'         if i==1:             print 'you have '+`i`+' more chance!'         time.sleep(1)         os.system('cls' if os.name=='nt' else 'clear')         print list  if '-' not in list:     print 'you win!!' else:     print 'game over!!'  x = raw_input('press enter') 

if need index of every character occurence:

indexes = [idx idx, ch in enumerate(word) if ch == x] 

perhaps should use unidecode keep accents in words, might useful depending on language (if not english). also, can use str.lower() or str.upper() methods ensure every word , trial in same case.

the string module has useful constants (e.g. ascii_uppercase).

however, in game don't need worry index. i've made version you:

#!/usr/bin/env python string import ascii_uppercase  word = "megalopolis".upper() # top-secret! trial = 3 # total trials available (number of mistakes lose game) typed = set() # typed characters word_letters = set(word)  while trial:     print     print "".join(ch if ch in typed else "-" ch in word)      # winning condition     if typed.issuperset(word_letters):         break      # data input     x = raw_input("enter letter: ").upper()      # error cases     if len(x) != 1:         print "invalid input."         continue     if x in typed:         print "already typed."         continue     if x not in ascii_uppercase:         print "what typed isn't letter."         continue      # valid data cases     typed.add(x)     if x in word:         print "good choice!"     else:         print "{} not found!".format(x),         trial -= 1         if trial == 1:             print "you have 1 more chance!"         elif trial > 1:             print "you have {} more chances.".format(trial)         else:             print 'sorry...'  # ending message print if trial:     print "you win!!" else:     print "game over!!" 
  1. hashbang: shebang should start "#!/". you're using windows, "bin" relative directory wasn't used you.
  2. "l" / l variable name should avoided! might seen 1 or lower "l" (pep8), or pipe "|". ps: @ beginning of item, typed same letter here twice.
  3. there's no need use "list" variable name here, , shouldn't do, that's built-in name.
  4. multiplication "txt" * 3 returns "txttxttxt" (it repeats data) both strings , lists
  5. neither "cls" nor "clear" worked here, showing

    "term environment variable not set."

    instead of clearing console screen. replaced these empty "print", , removed time sleep. subprocess if want call console (although i'd curses if there's need cli visualization).

  6. suppose x string. when x == "", bool(x) false, else bool(x) true.
  7. suppose x integer. when x == 0, bool(x) false, else bool(x) true.
  8. avoid backticks (`). no 1 uses them today in python, doesn't exist in python 3 , can use repr built-in instead. however, wanted str(trial), "%d" % trial or "{}".format(trial).
  9. the last "press enter" has operating system "auto-close-after-finish" behaviour, [at least] didn't need store in x.
  10. i've used generator expression. should read here list comprehensions if "for" in middle of 1 line confusing you. python developers use generator expressions , list comprehensions time, shouldn't avoid learning them.
  11. i replaced original winning evaluation comparison between set of characters word has , set of typed characters (both uppercase).

if there's here didn't understand, please ask new question.


Comments

Popular posts from this blog

assembly - 8086 TASM: Illegal Indexing Mode -

Java, LWJGL, OpenGL 1.1, decoding BufferedImage to Bytebuffer and binding to OpenGL across classes -

javascript - addthis share facebook and google+ url -