【问题标题】:Hangman game project刽子手游戏项目
【发布时间】: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


【解决方案1】:

首先让我们分析一下你的代码:

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,然后使用列表推导将索引填充到它。

                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. 

也不需要 break :计数增加,因此 while 将终止。如果它是用于外部 if/else ,则 break 在循环之外不起作用。

hangman()

OP 问题:

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

【讨论】:

  • 这就是应该向新学习者提供帮助的方式,为您提供非常有用和有用的答案+1
猜你喜欢
  • 2015-05-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-26
  • 2014-10-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多