【发布时间】:2019-08-27 09:56:53
【问题描述】:
我正在尝试一个刽子手游戏。我已经完成了设置游戏的基础知识(列表,与用户的交互),但我不知道如何为游戏做台词,不断询问和打印用户正确答案是什么,并说明刽子手。 我使用索引来搜索单词中字母的确切位置,但我不知道打印它,这取决于数字,而且我也不知道如何编写程序来跟踪正确的单词。
我会非常高兴您的帮助。也感谢您的耐心等待。 代码的第一部分编码良好,但 stackoverflow 显示不正确。
---------------------------------------------- --------------------------
import random
def hangman():
words = ['house','car','women', 'university','mother', 'school', 'computer','pants'] #list of words
computer = words[random.randint(0,6)] #computerchoice
word = list(computer) #Make a list with each letter of the word.
welcome = (input ('Welcome, type ok to continue: '))
if welcome == 'ok':
length = len(word)
print(f'help? the word has {length} letters')
option = (input('Start guessing, letter by letter'))
count= 0 #count takes the count of how many tries.
chances = length+3 #You are able to make 3 mistakes.
while count<=chances:
if option in word: #if the choice is there
place = word.index(option) #search for the place.
print() #dont know how to print it in 'that' place.
#place the correct letter over that line.
print('_ '*length) #Trying to do the under lines.
count+=1
else:
break
#Dont know to ilustrate the hangman depending on the length of the word.
hangman()
【问题讨论】:
-
编辑您的问题,突出显示您的代码并按下
{}按钮。它将被很好地格式化 -
@Hoog
{}是 2 个按钮?或者突出显示所有代码并点击ctrl+k -
@MattB 在我看来它只有一个按钮,但快捷方式
CTRL+K是一个更好的主意
标签: python python-3.x