【发布时间】:2020-07-20 11:39:45
【问题描述】:
import random
import sys
words=["tumble","sigh","correction","scramble","building","couple","ton"]
computer=random.choice(words)
attm=7
chosen_word=len(computer)*"*"
print(chosen_word)
while attm>0:
print(computer)
print(chosen_word)
player_guess=str(input("guess: "))
if len(player_guess)>1:
player_guess=str(input("enter one character only: "))
if player_guess in computer:
print("you're right")
attm==attm
for i in chosen_word:
player_guess=chosen_word.replace(chosen_word,player_guess)
print(chosen_word)
else:
print("wrong!")
attm-=1
print("attempts= ",attm)
if attm==0:
print("you lost")
sys.exit
我希望每当玩家猜到它在 selected_word 中被替换时,正确的字符替换星星 如果单词是“ton”,它会像这样显示 *** 如果玩家猜的是 (t) 选择的单词变成 (t**) 等等 简单的语法更可取,因为我是 python 新手
【问题讨论】:
-
您有问题吗?
-
@ForceBru 抱歉,这很令人困惑,但我尽力描述了这个问题,每次猜测正确时如何用字符替换星号
-
这部分问题自然属于它自己的函数——一个将所选单词和猜测的字母作为输入并返回要显示为输出的字符串。编写、测试和调试该功能——然后使用它。试图在不使用任何函数的情况下做一个刽子手游戏会导致代码不可读。至于如何做——只需在迭代所选单词时构建字符串。
标签: python arrays python-3.x list