By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,689 Members | 1,101 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,689 IT Pros & Developers. It's quick & easy.

Game advise Python

P: 1
Hi there, I'm learning python by myself and I've just come to big stop with my first project for a Hang/man. I need some help. Thank you.

Here's my code:

Expand|Select|Wrap|Line Numbers
  1. def get_secret():
  2.     initial = raw_input('enter string: ')
  3.     word = initial.lower()
  4.     print '\n'*60
  5.     return str(word)
  6.  
  7. def do_turn(display, misses):
  8.     print 'Word so far: ', display
  9.     print 'Missed :', misses
  10.     flag = False
  11.     while flag == False:
  12.  
  13.         letter = str(raw_input('Pick a letter to guess: '))
  14.         if len(letter)==1:
  15.             flag = True
  16.         else:
  17.             print 'Cmon man, enter ONE letter'
  18.         return letter
  19.  
  20.  
  21. def new_display(secret, display, letter):
  22.     temp = ''
  23.     for i in range(len(secret)):
  24.         if secret[i] in letter:
  25.             temp = temp[:i] + secret[i] + temp[i+1:]
  26.  
  27.     for letter in temp:
  28.         print (letter), display
  29.     return temp
  30.  
  31.  
  32.  
  33.  
  34. secret = get_secret()
  35. display = len(list(secret))*'_ '
  36. misses = 0
  37. correct = ''
  38. tries = 0
  39.  
  40. #print 'This is the word: ', display
  41. print list(secret) #this actually converts the secret to a list
  42.  
  43. for i in range(8):
  44.     letter = do_turn(display, misses)
  45.     guess = new_display(secret, display, letter)
  46.     while False:
  47.  
  48.         if letter in guess:
  49.             print 'bravo'
  50.  
  51.  
It's mostly that I can't get the function newdisp to replace the underscore with a letter that's been guessed (and replace all same letters).
For the missing I will do it in the main.
Jul 5 '12 #1
Share this Question
Share on Google+
1 Reply

Expert 100+
P: 626
This will only ask for a letter one time, no matter what is entered.
Expand|Select|Wrap|Line Numbers
  1.     while flag == False:
  2.           letter = str(raw_input('Pick a letter to guess: '))
  3.          if len(letter)==1:
  4.              flag = True
  5.          else:
  6.              print 'Cmon man, enter ONE letter'
  7.          return letter 
You declared "temp" as an empty string earlier so you are slicing an empty string.
Expand|Select|Wrap|Line Numbers
  1.         if secret[i] in letter:
  2.              temp = temp[:i] + secret[i] + temp[i+1:] 
  3. #
  4. #
  5.     display_list = list(display)
  6.     for ctr in range(len(secret)):
  7.         if secret[ctr] == letter:
  8.             ## only replaces first letter found
  9.             display_list[ctr] = letter
  10.             return "".join(display_list)
Jul 5 '12 #2

Post your reply

Sign in to post your reply or Sign up for a free account.