Python hangman game help.

This is the place for queries that don't fit in any of the other categories.

Python hangman game help.

Postby 09luke1 » Tue Nov 05, 2013 10:39 am

I have created a simple hangman game using some resources I've found on the internet, for this code I would like the user to be able to input their own words at the end of the hangman game so for example, when they have finished a game I would like the code to prompt them "Would you like to input your own words to the game?" I was wondering if anybody knew how to do this, or can lead me into the right direction, I'm a begginer at python :P :lol:

Code: Select all
import random

name = input("What is your name? ")
print ("Hello, " +name, "It's time to play hangman!")


countryName = ['england','spain','japan','korea','china','germany','france']
word = random.choice(countryName)
guesses = ''
turns = 6

while turns > 0:
  failed = 0
  for letter in word:
    if letter in guesses:
      print (letter)
    else:
      print ("_ ",end=' ')
      failed += 1
  print

  if failed == 0:
    print ('You win!')
    break

  guess = input('guess a letter: ')
  guesses += guess

  if guess not in word:
    turns -= 1
   
    print ('Sorry no')
    print ("You have", + turns, "more guesses")
    if turns < 6: print ('   O   ')
    if turns < 5: print (' \_|_/ ')
    if turns < 4: print ('  ;|;  ')
    if turns < 3: print ('  / \  ')
    if turns < 2: print (' -   - ')
    if turns < 1: print (' E N D ')
    if turns == 0:
      print ('The answer is', + word)

Last edited by Mekire on Tue Nov 05, 2013 11:03 am, edited 1 time in total.
Reason: First post lock.
09luke1
 
Posts: 1
Joined: Tue Nov 05, 2013 10:34 am

Re: Python hangman game help.

Postby Kebap » Tue Nov 05, 2013 1:14 pm

Hey luke, welcome to python and the forum! :)

This is an interesting question. It would be relatively easy to ask for new words and play a new round with these words. But if you end the program, all new words would be lost. If you want to also save the words for the next time you start the program, you would have to learn about data management. This can mean for example, maintaining a save file for all words.
Learn: How To Ask Questions The Smart Way
Join the #python-forum IRC channel on irc.freenode.net and chat with uns directly!
Kebap
 
Posts: 400
Joined: Thu Apr 04, 2013 1:17 pm
Location: Germany, Europe

Re: Python hangman game help.

Postby micseydel » Tue Nov 05, 2013 5:31 pm

Kebap wrote:But if you end the program, all new words would be lost. If you want to also save the words for the next time you start the program, you would have to learn about data management. This can mean for example, maintaining a save file for all words.

I would recommend a simple file and JSON to accomplish this, in this simple example.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1443
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 2 guests