限制字符数

时间:2020-01-18 14:40:46

标签: python-3.x

我想知道如何限制玩家可以放置多少个角色 试图限制每个答案一个字母。 这是一个猜谜游戏,但计算机只能回答是或否。 五次猜字母的机会。 然后尝试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

1 个答案:

答案 0 :(得分:2)

希望这就是您想要的:

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

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:   ")
相关问题