Advertisement
uottawamakermobile

Hangman(prints on one line)

Apr 7th, 2020
300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.34 KB | None | 0 0
  1. import random
  2. # library that we use in order to choose
  3. # on random words from a list of words
  4.  
  5. name = input("What is your name? ")
  6. # Here the user is asked to enter the name first
  7.  
  8. print("Good Luck ! ", name)
  9.  
  10. words = ['rainbow', 'computer', 'science', 'programming',
  11.         'python', 'mathematics', 'player', 'condition',
  12.         'reverse', 'water', 'board', 'geeks']
  13.  
  14. # Function will choose one random
  15. # word from this list of words
  16. word = random.choice(words)
  17.  
  18.  
  19. print("Guess the characters")
  20.  
  21. guesses = ''
  22.  
  23. # any number of turns can be used here
  24. turns = 12
  25.  
  26.  
  27. while turns > 0:
  28.    
  29.     # counts the number of times a user fails
  30.     failed = 0
  31.    
  32.     # all characters from the input
  33.     # word taking one at a time.
  34.     for char in word:
  35.        
  36.         # comparing that character with
  37.         # the character in guesses
  38.         if char in guesses:
  39.             print(char, end='') #<-------------------------------------------------------------------------------------------------
  40.            
  41.         else:
  42.             print("_ ", end='')  #<-------------------------------------------------------------------------------------------------
  43.            
  44.             # for every failure 1 will be
  45.             # incremented in failure
  46.             failed += 1
  47.            
  48.  
  49.     if failed == 0:
  50.         # user will win the game if failure is 0
  51.         # and 'You Win' will be given as output
  52.         print("You Win")
  53.        
  54.         # this print the correct word
  55.         print("The word is: ", word)
  56.         break
  57.    
  58.     # if user has input the wrong alphabet then
  59.     # it will ask user to enter another alphabet
  60.     print('')  #<-----------------------------------------------------------------------------------------------------------------------
  61.     guess = input("guess a character:")
  62.    
  63.     # every input character will be stored in guesses
  64.     guesses += guess
  65.    
  66.     # check input with the character in word
  67.     if guess not in word:
  68.        
  69.         turns -= 1
  70.        
  71.         # if the character doesn’t match the word
  72.         # then “Wrong” will be given as output
  73.         print("Wrong")
  74.        
  75.         # this will print the number of
  76.         # turns left for the user
  77.         print("You have", + turns, 'more guesses')
  78.        
  79.        
  80.         if turns == 0:
  81.             print("You Loose")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement