如何为我的《 Hang Man》游戏改进代码python代码?

时间:2018-11-01 05:12:39

标签: python-3.x

有什么我可以更改或修复的代码使之更具吸引力吗?或更有效? 到目前为止,它以我想要的方式工作。 ps。 lexicon_file充满了一堆单词

import sys
import random

lexicon_file = open(sys.argv[1])

print("Welcome to Console Hangman!")

#select a random word that contained AT LEAST four letters
with lexicon_file as file:
    contents = [words.strip() for words in file]
    while True:
        randomword = random.choice(contents)
        if len(randomword) >= 4:
            break

#amt of guesses we have
lives = 8
#storing right and wrong guesses
right = []
wrong = []

while lives > 0:
    blank = ""
    for letter in randomword:
        if letter in right:
            blank += letter
        else:
            blank += " _"
    if blank == randomword:
        break
    print("\nThe secret word looks like: ", blank)
    print("You have", lives, "guesses remaining.")
    guess = input("What's your next guess? ")
    guess = guess.lower()
    if guess in right or guess in wrong:
        print("Already guessed,", guess)
    elif guess in randomword:
        print("Yay!\n")
        right.append(guess)
    else:
        print("Aw man, sorry there is no " + '"' + guess + '"\n')
        lives -= 1
        wrong.append(guess)

if lives:
    print("Congratulations!\nYou guessed the word:", randomword)
else:
    print("Oh no, you didn't get it!\nThe secret word was:", randomword)

0 个答案:

没有答案