【问题标题】:How do I make my "Lingo" game in python 3.5 work?如何在 python 3.5 中使我的“Lingo”游戏正常工作?
【发布时间】:2016-11-27 23:24:32
【问题描述】:

游戏与行话略有不同,因为可以使用任何字长。我怎样才能让我的程序打印一个?当字母在单词中但不在正确的位置时?如果字母在正确的位置,我如何让它打印实际的字母?例如单词是“tank”并且用户输入“town”它应该打印“t--?”我让一切正常工作,直到我必须让我的代码输出除“-”以外的字符 (这些词是荷兰语,但这不应该有任何区别)

import random
wordlist = ["hond", "haas", "neus", "peer","fruit", "laptop","raam","computer", "python", "hakan", "akkas", "mohammed", "amine", "school","informatica"]
word = random.choice(wordlist)
print("Your secret word has ", len(word), "letters")
while True:
    guessword = input("Guess the word:")
    if len(Guessword) != len(word): print("type a word with", len(word),"letters")
    elif guessword == word: break
    elif guessword != word:
        if guessword != word:  print("-" * len(word))
print("Congratulations, you guessed the word correctly!")

我还必须让游戏告诉你猜单词需要多少回合,并实施基于休息时间的评分系统。

编辑:必须尽可能紧凑

【问题讨论】:

    标签: python-3.x


    【解决方案1】:

    一种使用enumerate的方法:

    import random
    wordlist = ["hond", "haas", "neus", "peer","fruit", "laptop","raam","computer", "python", "hakan", "akkas", "mohammed", "amine", "school","informatica"]
    # 'word' hardcoded for testing
    word = "tank" #random.choice(wordlist)
    print("Your secret word has ", len(word), "letters")
    while True:
        guessword = input("Guess the word:")
        if len(guessword) != len(word): print("type a word with", len(word),"letters")
        elif guessword == word: break
        elif guessword != word:
            for position, letter in enumerate(guessword):
                if letter == word[position]:
                    print(letter, end="")
                elif letter not in word:
                    print("-", end="")
                else:
                    print("?", end="")
            print("")
    print("Congratulations, you guessed the word correctly!")
    

    测试:

    $ python3.5 word_game.py 
    Your secret word has  4 letters
    Guess the word:town
    t--?
    Guess the word:tans
    tan-
    Guess the word:tank
    Congratulations, you guessed the word correctly!
    

    【讨论】:

    • 谢谢,帮了大忙!但是我如何实现一个评分系统并告诉玩家他们猜单词花了多少轮?
    • @MohammedElKahtaoui 在循环外部(之前)添加一个计数器值,在elif guessword != word: 中递增并在print("Congratulations, you guessed the word correctly!") 之后打印它
    • 好的,我实现了得分,并实现了代码询问玩家姓名的一行。如何制作一个自我更新的高分列表(高分列表中最多 5 个名字)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-04-07
    • 2012-07-04
    • 1970-01-01
    • 2017-08-13
    • 1970-01-01
    • 1970-01-01
    • 2021-02-27
    相关资源
    最近更新 更多