首页 > 解决方案 > 限制字符数

问题描述

我想知道如何限制玩家可以思考的角色数量,以限制每个答案一个字母。这是一个猜谜游戏,但计算机只能回答是或否。五次猜测字母的机会。然后需要在 5 次尝试后猜出正确的单词。

import random

words = dict(
        helium = "type of element",
        korea="a country in asia",
        peugeot="brand of a car",
        bournemouth="a good place for holiday")

word=list(words)
choice=random.choice(word)
x=list(choice)
score=0
chance=5


print("\n\n\t\t\t WELCOME TO GUESSING GAME")
print("\t\t\tYOU HAVE 5 CHANCES TO GUESS THE WORD")
print("\nit has a", len(choice),"letters word")
print("and this is a clue", (words[choice]))


while word:
    guess = input("is there a letter :")
    if guess in choice:
        print("yes")
    else:
        print("no")

    score +=1

    if score == chance:
        print("time to guess the right word")
        guess = input("and the word is :")
        if guess == choice:
            print("well done you guess the right word which is ", choice.upper())
            break
        else:
            print("better luck next time the right word is ", choice.upper())
            break

标签: python-3.x

解决方案


希望这是你想要的:

首先,您应该创建一个新功能:

def own_input(text=""):
    user_input = ""
    # While the length of the input string is not 1
    while len(user_input) != 1:
         # Ask user for input with message
         user_input = input(text)

现在你可以像这样使用它:

a = own_input("Character:   ")

推荐阅读