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
解决方案
希望这是你想要的:
首先,您应该创建一个新功能:
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: ")
推荐阅读
- javascript - 获取带有页面的数据表的所有复选框
- regex - Presto (AWS Athena) 中的拆分和搜索逗号分隔列
- pseudocode - 使用 for 循环以简单的伪代码将值分配给数组中的元素
- sql - 了解窗口函数以在保留真实更改的同时删除重复记录
- python - 如何使用 ConfigObj 附加到现有配置文件?
- python - 是否检查 int 并继续在 python 中进行交易
- css - CSS网格对齐项目的大小不同
- c++ - 如何在 C++ 中通过引用传递结构?
- c# - 是否可以一项一项地运行我的作业(基于 Scoped Background Services)?
- r - 将基于行中重复项的值分离到 R 中的新列中