Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- import string
- victory_counter, loss_counter = 0, 0
- print("H A N G M A N\n")
- while True:
- list_of_words = ["python", "java", "swift", "javascript"]
- random_choices = list_of_words[random.randrange(0,4)]
- user_attempts = 8
- a_word_to_guess = list(random_choices)
- list_with_lettrs = ["-" for _ in range(len(a_word_to_guess))]
- marker, users_letter = "", ""
- menu_selection = input('Type "play" to play the game, "results" to show the scoreboard, and "exit" to quit: ')
- if menu_selection == "exit":
- break
- elif menu_selection == "results":
- print("You won: {} times.\nYou lost: {} times.".format(victory_counter, loss_counter))
- continue
- while user_attempts > 0 and menu_selection == "play":
- print("\n{}".format("".join(list_with_lettrs)))
- user_inp = input("Input a letter: ")
- if len(user_inp) == 1:
- if user_inp not in string.ascii_lowercase:
- print("Please, enter a lowercase letter from the English alphabet.\n")
- continue
- else:
- if user_inp in users_letter:
- print("You've already guessed this letter.\n")
- continue
- else:
- print("Please, input a single letter.\n")
- continue
- for chr in range(len(a_word_to_guess)):
- if a_word_to_guess[chr] == user_inp:
- list_with_lettrs[chr] = user_inp
- if list_with_lettrs.count(list_with_lettrs[chr]) == 1:
- marker = "Yes"
- if user_inp not in a_word_to_guess:
- print("That letter doesn't appear in the word.\n")
- user_attempts -=1
- else:
- print()
- users_letter += user_inp
- if list_with_lettrs == a_word_to_guess:
- print("You guessed the word {}!\nYou survived!".format("".join(list_with_lettrs)))
- victory_counter += 1
- break
- elif user_attempts == 0:
- if a_word_to_guess== list_with_lettrs:
- victory_counter += 1
- print("You guessed the word {}!\nYou survived!".format("".join(list_with_lettrs)))
- else:
- loss_counter += 1
- print("You lost!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement