python - Hangman code python3 -


i have python 3 hangman code works not know how can allow user select difficulty (easy (9), medium (7), hard(5)) in main function. there simpler code used this?

import random  wordlist = ("phone", "laptop", "desktop", "sewer", "television", "never", "guess", "nice", "chair", "car"); word = random.choice(wordlist) acceptable = ("abcdefghijklmnopqrstuvwxyz")  guessed = [] state = 0 haswon = 0 playedonce = 0  def menu ():     print("""     1. easy (9 misses)     2. medium (7 misses)     3. hard (5 misses)     """)     return int(input("what level want play?:"))  def wantstoplay():     if (not playedonce):         return 1     l = input("\nwould play again? (y/n)")     while (l != "y" , l != "y" , l != "n" , l != "n"):         l = input("\nwould play again? (y/n)")     if (l.lower() == "y"):         return 1     return 0  def takenewletter():     global state, haswon     newprint("so far, have guessed following letters...")     g in guessed:         print(g, end=" ")     letter = input("\n\nwhat letter guess next?\n")     while (letter in guessed or letter not in acceptable):         if (len(letter) > 1):             if (letter.lower() == word.lower()):                  newprint("you win!")                  haswon = 1                  break             else:                 newprint("boo... wrong... you're dead...")                 state = 7                 break         else:             if (letter not in acceptable):                 letter = input("that character unacceptable. many enter lower case letters.\n")             else:                 letter = input("you have guessed letter, try one...\n")     guessed.append(letter)     if (letter not in word):         state += 1     return  def drawword():     tempword = ""     c in word:         if (c in guessed):             tempword += c + " "         else:             tempword += "_ "     newprint(tempword)     return  def drawstickman():     if (state >= 7):         print("   _______")         print("|/      |")         print("|      (_)")         print("|      \|/")         print("|       |")         print("|      / \\")         print("|")         print("|___")         print("oops. you're dead.")     elif (state == 6):         print("   _______")         print("|/      |")         print("|      (_)")         print("|      \|/")         print("|       |")         print("|      / ")         print("|")         print("|___")     elif (state == 5):         print("   _______")         print("|/      |")         print("|      (_)")         print("|      \|/")         print("|       |")         print("|")         print("|")         print("|___")     elif (state == 4):         print("   _______")         print("|/      |")         print("|      (_)")         print("|      \|/")         print("|")         print("|")         print("|")         print("|___")     elif (state == 3):         print("   _______")         print("|/      |")         print("|      (_)")         print("|      \|")         print("|")         print("|")         print("|")         print("|___")     elif (state == 2):         print("   _______")         print("|/      |")         print("|      (_)")         print("|")         print("|")         print("|")         print("|")         print("|___")     elif (state == 2):         print("   _______")         print("|/      |")         print("|")         print("|")         print("|")         print("|")         print("|")         print("|___")     elif (state == 1):         newprint("as first mistake, let off...")         print("   _______")         print("|/")         print("|")         print("|")         print("|")         print("|")         print("|")         print("|___")     elif (state == 0):         print("   _______")         print("|/")         print("|")         print("|")         print("|")         print("|")         print("|")         print("|___")  def hasguessed():     if (haswon == 1):         return 1     if (state >= 7):         return 1     c in word:         if (c not in guessed):             return 0     if (len(guessed) == 0):         return 0     return 1  def setup_game():     newprint("welcome hangman game!")     newprint("i have chosen random word super secret list, try guess before stickman dies!")  def newprint(message, both = 1):     msg = "\n" + message     if (both != 1):         msg += "\n"     print(msg)  def main():     global guessed, haswon, state, playedonce, word, wordlist     setup_game()     newprint("my word " + str(len(word)) + " letters long.")     while (wantstoplay() == 1):         word = random.choice(wordlist)         guessed = []         playedonce = 1         haswon = 0         state = 0         while (hasguessed() == 0 , state < 7):             drawstickman()             drawword()             takenewletter()         drawstickman()         newprint("my word " + word)  main() 

what need number of misses player can made. i've made menu() return value:

def menu():     print("""     1. easy (9 misses)     2. medium (7 misses)     3. hard (5 misses)     """)     while true:         level = input("what level want play?:")         if level in ('1', '2', '3'):             return {'1': 9, '2': 7, '3': 5}[level]         print('wrong answer!') 

now should somehow put menu , result main program. call menu function after setup_game() , replace contant 7 result.

def main():     global guessed, haswon, state, playedonce, word, wordlist     setup_game()     allowed_misses = menu()     ...         while (hasguessed() == 0 , state < allowed_misses) 

you should remove statecheck hasguessed() since redundant , uses constant value.


Comments

Popular posts from this blog

PHPMotion implementation - URL based videos (Hosted on separate location) -

javascript - Using Windows Media Player as video fallback for video tag -

c# - Unity IoC Lifetime per HttpRequest for UserStore -