gpt4 book ai didi

python - 使用 python 执行绞刑吏程序

转载 作者:行者123 更新时间:2023-12-01 05:39:28 26 4
gpt4 key购买 nike

我需要用 python 程序制作一个刽子手程序,但我不知道如何继续。在节目中我有 6 次机会(也称为“生命线”)来猜测单词中的字母。每一次错误的猜测都会缩短“生命线”。当您猜对单词或用完所有“生命线”时,游戏就会结束。这是示例输出:

['_', '_', '_', '_']

***Life Line***
-+-+-+-+-+-+
$|$|$|$|$|$|
-+-+-+-+-+-+

Enter your guess: a
['_', '_', '_', '_']

***Life Line***
-+-+-+-+-+
$|$|$|$|$|
-+-+-+-+-+

Incorrect Guess: ['a']
...................................
Enter your guess: b
['b', '_', '_', '_']

***Life Line***
-+-+-+-+-+
$|$|$|$|$|
-+-+-+-+-+

Incorrect Guess: ['a']
...................................
Enter your guess: l
['b', 'l', '_', '_']

***Life Line***
-+-+-+-+-+
$|$|$|$|$|
-+-+-+-+-+

Incorrect Guess: ['a']
...................................
Enter your guess: o
['b', 'l', '_', '_']

***Life Line***
-+-+-+-+
$|$|$|$|
-+-+-+-+

Incorrect Guess: ['a', 'o']
...................................
Enter your guess: u
['b', 'l', 'u', '_']

***Life Line***
-+-+-+-+
$|$|$|$|
-+-+-+-+

Incorrect Guess: ['a', 'o']
...................................
Enter your guess: e
['b', 'l', 'u', 'e']

***Life Line***
-+-+-+-+
$|$|$|$|
-+-+-+-+

Incorrect Guess: ['a', 'o']
...................................
You Got it Right! Well Done!

我已经输入了前几个代码,但卡住了。

import random
wordList = ["Mary","Tian Pei","Pong"]
randname = random.choice ( wordList)
print randname

resultList = [ ]

for i in range(len(randname)):
resultList.append("_")
print resultList

最佳答案

创建空白列表:

>>> name = "Mary"
>>> blanks = ["_" for letter in name]
>>> blanks
['_', '_', '_', '_']

创建错误猜测列表:

>>> incorrect_guesses = # you figure this one out

设置你的生命线:

>>> life_lines = # you figure this one out

提示猜测:

>>> guess = raw_input("Guess: ")
Guess: a
>>> guess
'a'

保存一个变量来说明猜测是否不正确:

>>> incorrect = # you figure this one out

迭代name,如果name中的相应字母与name中的相应字母相同,则将blanks中相应的空行替换为该字母猜测:

>>> for i in range(len(name)):
... if name[i] == guess:
... incorrect = # you figure this one out
... blanks[i] = # you figure this one out
...
>>> blanks
['_', 'a', '_', '_']

如果不正确True,则将猜测添加到in Correct_guesses(在本例中,因为猜测是正确的,它不会更新)并减去生命线:

>>> if incorrect:
... incorrect_guesses.append( # you figure this one out )
... life_lines -= # you figure this one out
...

要检查是否相等,请将空格中的字母连接起来以重新构成原始单词,以便可以比较两者:

>>> final = ''.join(blanks)    # this connects each letter with an empty string
>>> final
'_a__'

一般控制结构可能如下:

choose random word
create blanks list
set up life-lines
while life_lines is greater than 0 and word not completed:
print blanks list
print life_lines graphic
if there are incorrect guesses:
print incorrect guesses
prompt for guess
check if correct and fill in blanks
if incorrect:
add to incorrect guesses and subtract a life-line
if word completed:
you win
else:
you lose

由您来填写我在这里写的空白(并编写您自己的例程来打印生命线图形等)。

关于python - 使用 python 执行绞刑吏程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17983653/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com