Advertisement
Guest User

Untitled

a guest
Aug 12th, 2017
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.34 KB | None | 0 0
  1. #!/usr/bin/env python2
  2.  
  3. from random import *
  4.  
  5. player_score = 0
  6. computer_score = 0
  7.  
  8. def hangedman(hangman):
  9.     graphic =[ """ base""","""first attempt made""", """ second attempt""","""third attempt ""","""fourth attempt """,""" fifth attempt """,""" make your sixth and final attempt!"""]
  10.     print graphic[hangman]
  11.     return
  12.  
  13. def start():
  14.     print "Let's play a game of Linux Hangman"
  15.     while game():
  16.         pass
  17.     scores()
  18.  
  19. def game():
  20.     dictionary=["gentoo", "ubuntu", "arch", "slackware", "fedora", "suse"]
  21.     word = choice(dictionary)
  22.     word_length = len(word)
  23.     clue = word_length * ["_"]
  24.     tries = 6
  25.     letters_tried = ""
  26.     guesses = 0
  27.     letters_right = 0
  28.     letters_wrong = 0
  29.     global computer_score, player_score
  30.  
  31.     while (letters_wrong != tries) and ("".join(clue) != word):
  32.         letters=guess_letter()
  33.         if len(letter)==1 and letter.isalpha():
  34.             if letters_tried.find(letter) != -1:
  35.                 print "You've already picked", letter
  36.             else:
  37.                 letters_tried = letters_tried + letter # what about using letters_tried += 1 instead ???
  38.                 first_index=word.find(letter)
  39.                 if first_index == -1:
  40.                     letters_wrong += 1
  41.                     print "Sorry,", letter, "isn't what we're looking for."
  42.                     # what about swapping lines 40 with 41  - - would that behave the same way?
  43.                 else:
  44.                     print "Congratulations", letter, "is correct."
  45.                     for i in range(word_length):
  46.                         if letter == word[i]:
  47.                             clue[i] = letter
  48.         else:
  49.             print "Go ahead, choose another."
  50.  
  51.         hangedman(letters_wrong)
  52.         print " ".join(clue)
  53.         print "Guesses: ", letters_tried
  54.  
  55.         if letters_wrong == tries:
  56.             print "Game Over"
  57.             print "The word was, ", word
  58.             computer_score += 1
  59.             break
  60.         if "".join(clue) == word:
  61.             print "You Win!"
  62.             print "The word was, ", word
  63.             player_score += 1
  64.             break
  65.     return play_again()
  66.  
  67. def guess_letter():
  68.     print
  69.     letter = raw_input("Take a guess at the mystery word.")
  70.     letter.strip()
  71.     letter.lower()
  72.     print
  73.     return letter
  74.  
  75. def play_again():
  76.     answer = raw_input("Would you like to play again? y/n:")
  77.     if answer in ("y", "Y", "yes", "Yes", "Of course!"):
  78.         return answer
  79.     else:
  80.         print "Thank you very much for playing our game. See you next time!"
  81.  
  82. def scores():
  83.     global player_score, computer_score
  84.     print "HIGH SCORES"
  85.     print "Player: ", player_score
  86.     print "Computer: ", computer_score
  87.  
  88. if __name__ == '__main__':
  89.     start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement