473,698 Members | 2,371 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Game advise Python

1 New Member
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
1 1375
dwblas
626 Recognized Expert Contributor
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

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

Similar topics

1
3687
by: Arnaud Delobelle | last post by:
Hi all, I have written a little while ago in Python a game whose aim is to program a (or several) robots so that they move around in a maze and collect some flags. They only have a limited amount of instructions at their disposal (depending on the level). It is meant to be an introduction to two things: - programming
6
636
by: Darryl | last post by:
Hi, I've been programming in Python for a couple of weeks now and am quite impressed with the language - it was very easy to learn (it actually reminds me a lot of programming in BASIC many many years ago - it's very fast to hack together a couple lines of code in interactive mode). I'm throwing together a simple little game (sort of a MUD-type thing) as my first 'big' project, and I thought it would be cool to be able to script the...
57
4237
by: John Howard | last post by:
I've sent several messages over the last year asking about python - Who teaches python? Is python losing steam? etc. I have noticed, eg, the declinng number of books at my local borders. The last time I visited a borders (last week), there was 1 (sic) book about python on the shelve compared to dozens on perl & java! On my last inquiry about who teaching python, I got two, maybe three, responses. I really want to see python succeed! It's...
5
2563
by: Tlo | last post by:
hello, i would like to do the following, and as i had never used python in a network framework i would like to have opinions on this : i would like to provide some kind of network quizz game, each player logged in and can then join 'playing rooms', in each rooms they had to answers many quizz-like questions, after a game finished each players involved in it are ranked from their corrects answers and the time they spent to answer....
2
4141
by: flyaflya | last post by:
I want make a desktop game suports LAN connect, but pygame has nothing about network.How to let pygame suport LAN connect? have you some examples or articles about LAN connect?
159
13440
by: petantik | last post by:
Are there any commercial, or otherwise obfuscators for python source code or byte code and what are their relative advantages or disadvantages. I wonder because there are some byte code protection available for java and .NET, although from what i've read these seem to be not comprehensive as protection schemes
1
3690
by: Jerry Fleming | last post by:
Hi, I have wrote a game with python curses. The problem is that I want to confirm before quitting, while my implementation doesn't seem to work. Anyone can help me? #!/usr/bin/python # # Brick & Ball in Python # by Jerry Fleming <jerryfleming@etang.com>
1
1760
by: diffuser78 | last post by:
I have to create a small and simple GUI app in Python which is similar to a netwrok game. Let me expalain you. I need some suggestions. I might use wxPython for GUI because that is the only GUI oolkit I know. There are 6 players in the game. They are all connected though a LAN (possible an internal network). There is one main supervirsor who starts the game and other 5 are players. Once the supervisor starts the game, a move goes...
9
9651
by: prince99 | last post by:
Q) Design and implement a volleyball simulation. In volleyball, only the serving team can score, and if the receiving team win the rally, they gain the serve. Games are played to a score of 15, but must be won by at least 2 points. If the scores are closer, the game can continue indefinitely. Code given below plz correct it #volleyball.py def main (): print Intro () probA, probB, n = getInputs () winsA, winsB = simNGames...
2
13085
by: wweredhead | last post by:
I need help with this game I'm trying to do for my class out of a python book. This is what is says in the book. A two-player version of the game Nim. In the game, players take turns removing from 1 to 4 sticks from a pile of 13. The player who picks up the last stick wins the game. Your program should validate the input from the players. This means that the program should continue to ask a player for the number of sticks he or she wishes to...
0
9170
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9031
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8901
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6528
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4371
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3052
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2336
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.