首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >刽子手代码python3

刽子手代码python3
EN

Stack Overflow用户
提问于 2014-04-06 20:22:53
回答 1查看 2K关注 0票数 1

我有一个python 3挂人代码,但我不知道如何允许用户在主函数中选择困难(轻松(9)、中(7)、硬(5))。还有更简单的代码可以用于此吗?

代码语言:javascript
复制
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 do you want to play?:"))

def wantsToPlay():
    if (not playedOnce):
        return 1
    l = input("\nWould you like to play again? (y/n)")
    while (l != "y" and l != "Y" and l != "n" and l != "N"):
        l = input("\nWould you like to play again? (y/n)")
    if (l.lower() == "y"):
        return 1
    return 0

def takeNewLetter():
    global state, hasWon
    newPrint("So far, you have guessed the following letters...")
    for g in guessed:
        print(g, end=" ")
    letter = input("\n\nWhat letter would you like to 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... that was wrong... you're dead...")
                state = 7
                break
        else:
            if (letter not in ACCEPTABLE):
                letter = input("That character is unacceptable. You many only enter lower case letters.\n")
            else:
                letter = input("You have already guessed that letter, try another one...\n")
    guessed.append(letter)
    if (letter not in WORD):
        state += 1
    return

def drawWord():
    tempWord = ""
    for 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 this is your first mistake, I will let you 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
    for c in WORD:
        if (c not in guessed):
            return 0
    if (len(guessed) == 0):
        return 0
    return 1

def setup_game():
    newPrint("Welcome to the Hangman game!")
    newPrint("I have chosen a random word from my super secret list, try to guess it before your 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 is " + str(len(WORD)) + " letters long.")
    while (wantsToPlay() == 1):
        WORD = random.choice(WORDLIST)
        guessed = []
        playedOnce = 1
        hasWon = 0
        state = 0
        while (hasGuessed() == 0 and state < 7):
            drawStickman()
            drawWord()
            takeNewLetter()
        drawStickman()
        newPrint("My word was " + WORD)

main()
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-04-06 22:57:27

你真正需要的是一个球员的失误数。因此,我使用menu()来返回该值:

代码语言:javascript
复制
def menu():
    print("""
    1. Easy (9 misses)
    2. Medium (7 misses)
    3. Hard (5 misses)
    """)
    while True:
        level = input("What level do you want to play?:")
        if level in ('1', '2', '3'):
            return {'1': 9, '2': 7, '3': 5}[level]
        print('Wrong answer!')

现在你应该把这个菜单和它的结果放到你的主程序中。在setup_game()之后调用菜单函数,并将内容7替换为其结果。

代码语言:javascript
复制
def main():
    global guessed, hasWon, state, playedOnce, WORD, WORDLIST
    setup_game()
    allowed_misses = menu()
    ...
        while (hasGuessed() == 0 and state < allowed_misses)

您还应该从hasGuessed()中删除statecheck,因为它是多余的,并且使用一个常量值。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22899536

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档