[英]Python Guess the Word Game
Describing the game if you know what means word guessing dont bother to read描述游戏,如果你知道什么意思猜词不用费心去读
Hey.嘿。 I know I opened a question about that earlier but again Im stuck in another situation.我知道我之前就此提出了一个问题,但我又陷入了另一种情况。 I making a game that there is a list full of words and computer choses a random word in it.我制作了一个游戏,其中有一个充满单词的列表,计算机在其中选择了一个随机单词。 And program asking to person guess a letter.并要求人们猜一个字母的程序。 If it is in letter it should write the letter in exact order in that word.如果它是字母,它应该在那个词中按照准确的顺序写字母。 Lets say our word is word.可以说我们的话就是话。
If user input is "w", program will print "w___" and again如果用户输入的是“w”,程序将打印“w___”并再次打印
if user says "o" program will print "wo__" like that.如果用户说“o”,程序将像那样打印“wo__”。
What is the issue?问题是什么?
I did achieve to print if user input in that word and in which order in that word but when user guess again when he got right in first letter my print variable refreshing and prints just last guessed letter.如果用户输入该单词以及该单词的顺序,我确实实现了打印,但是当用户再次猜测时,当他在第一个字母中正确时,我的打印变量刷新并打印最后猜到的字母。
Let me explain让我解释
My word again "word" and user guess "w" letter first.我的单词又是“单词”,用户先猜“w”字母。
So program will print "w___" But when user guess again and lets say he/she guessed "o" this time.所以程序将打印“w___”但是当用户再次猜测并假设他/她这次猜到了“o”时。 Program prints just "_.o__" but not w.程序只打印“_.o__”而不是 w。
What I want to do我想做的事
if guess_count == 5 # word's total letter number: print("Win"), break the loop if guess_count == 5 # 单词的总字母数: print("Win"), break loop
if error_count == 3 # if player type 3 wrong letter: print("Lose"), break the loop if error_count == 3 # if player type 3 wrong letter: print("Lose"), 中断循环
My code我的代码
Create a loop创建循环
run = True
while run:
Create a namelist and chose a random name创建一个名单并选择一个随机名称
name_list = ["sound"]
pick_word = random.choice(name_list)
Create a while loop again # whole code is under this再次创建一个 while 循环# 整个代码都在这下面
while True:而真实的:
Get user input获取用户输入
user_input = input("Guess a letter: ") user_input = input("猜一个字母:")
Creating if statment if for if input len > 1 get error创建 if 语句 if for if input len > 1 get error
if len(user_input) == 1:
*Create a variable that gets user input in order of word as number. *创建一个变量,按照单词的顺序获取用户输入的数字。
(I cant say that sentence in english) (我不会用英语说那句话)
index = pick_word.index(user_input) index = pick_word.index(user_input)
Replace the order number to user input将订单号替换为用户输入
word_tracker[index] = user_input word_tracker[索引] = user_input
Create a string to print it创建一个字符串来打印它
word = "".join(word_tracker) word = "".join(word_tracker)
Print it打印出来
print(word)打印(单词)
Increase guess_count增加guess_count
count += 1计数 += 1
If guess_count is 5 break the loop如果 guess_count 为 5 则中断循环
if count == 5:如果计数 == 5:
If guess not in word如果猜不出来的话
except ValueError:
print("You couldnt guess the letters.")
guessing += 1
if guessing == 3:
print("Peh")
exit()
If guessed letter not 1 character如果猜到的字母不是 1 个字符
else:
print("You allowed to type just one letter")
I hope I wont get ban haha我希望我不会被ban哈哈
try changing尝试改变
word_tracker[index] = user_input
to到
word_tracker.append(user_input)
That is if 'word_tracker' is a list也就是说,如果 'word_tracker' 是一个列表
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.