子手游戏项目

时间:2019-04-05 13:20:07

标签: python python-3.x

我正在尝试执行子手游戏。我已经实现了设置游戏的基础知识(列表,与用户的交互),但是我不知道该如何做游戏的台词,不断询问和打印用户正确答案是什么以及掩盖子手。 我使用index来搜索单词中字母的确切位置,但是我不知道要打印它,具体取决于数字,而且我也不怎么编码程序会跟踪正确的单词。

您的帮助,我将非常高兴。也感谢您的耐心配合。 代码的第一部分编码正确,但是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()

1 个答案:

答案 0 :(得分:4)

首先让我们分析您的代码:

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. 

到目前为止,一切都很好,尽管str可以与list相同的方式使用,因此无需对其进行转换。

    welcome = (input ('Welcome, type ok to continue: '))
    if welcome == 'ok':
        length = len(word)
        print(f'help? the word has {length} letters') 

是的,但不是唯一字母。您可以使用set()获得唯一字母的数量。

        option = (input('Start guessing, letter by letter'))

如果您的输入从此处开始,则只要求输入一个字母,您需要将此部分包括在while循环中

        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. 

这只会给您第一次出现的索引。 我们应该记住对这种搜索使用正则表达式:Find all occurrences of a substring in Python

                print()   #dont know how to print it in 'that' place. 

请记住使用打印格式f'stufff{value}stuffff'

                #place the correct letter over that line.

要执行此操作,您只需要使用_创建一个str,然后使用list comprehension用索引将其填充。

                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. 

也不需要中断:计数递增,因此将终止。而且如果是外部if / else,则在循环外中断不起作用。

hangman()

关于OP的问题

您想整理一下自己的哪一点?接下来需要什么帮助?