【问题标题】:hangman graphics blank word creation and space removal刽子手图形空白词创建和空间删除
【发布时间】:2014-02-20 23:46:23
【问题描述】:
graphics=['''------------
             |         |        ''','''
------------
|         |  ''','''

------------
|         |         
|          O''','''
------------
|         | 
|          O 
|         / |''','''
------------
|         | 
|          O 
|         / | 
|          | ''','''
------------
|         |
|          O 
|         / |
|          |
|         / | 
|
|            ''']

print('Welcome to Hangman! Guess the mystery word with less than 6 mistakes!')



while True:
    words=['table','chair','pencil','stapler','pen',
          'computer','printer','cable','books','shelf']

    alphabet=['a','b','c','d','e','f','g,','h','i','j','k','l',
             'm','n','o','p','q','r','s','t','u','v','w','x','y','z']

    number=input('Please enter an integer number 
    (0<=number<10) to choose the word in the list:')    

    if number=='':
        print('Empty input!')
        continue
    elif number in alphabet:
        print('Input must be an integer!')
        continue

    number=int(number)

    if number<0 or number>9:
        print('Index is out of range!')
        continue

    elif 0<=number<10:
        break

words2=[]
words2.extend(words[number])


print('The length of the word is: ',len(words2))
print('')

i=0
j=0
x=0
while j<6 and i!=len(words2):

    letter=input('Please enter the letter you guess: ')

    for alphabet in letter:
        if alphabet in words2:
            print('The letter is in the word.')
            i=i+1

            if i==len(words2):
                    print('You have found the mystery word. You win!')
                    print('Goodbye!')
                    break
            else:
                continue       

        elif alphabet not in words2:
            if letter not in alphabet:
                print('You need to input a single alphabetic character!')
            elif letter not in words2:
                j=j+1
                print('The letter is not in the word.')
                print(graphics[j])

嗨!这是一个我几乎完成的刽子手游戏。我只有 2 个问题。

1- 图形和打印行之间有一个行间距。 例如->

字母不在单词中。


| |

我怎样才能消除这个差距?

2- 我想创建一个由'' 组成的空白单词,该单词的长度等于单词的长度,并且在正确的位置将“正确”字母替换为“”。我对如何做到这一点感到困惑。 我知道要使用列表替换功能,但我首先要如何创建一个空白单词?

如果这是我应该弄清楚或复杂的事情,你不必给我答案。请为我指明正确的方向。

谢谢! :)

【问题讨论】:

    标签: python


    【解决方案1】:
    graphics=['''------------
                 |         |        ''','''
    ------------
    |         |  ''','''
    
    ------------
    |         |         
    |          O''','''
    ------------
    |         | 
    |          O 
    |         / |''','''
    ------------
    |         | 
    |          O 
    |         / | 
    |          | ''','''
    ------------
    |         |
    |          O 
    |         / |
    |          |
    |         / | 
    |
    |            ''']
    

    graphics 中的每个字符串都以换行符开头。

    字符串中的换行符加上隐式换行符在文本和图形之间提供了一个空白行。

    重新格式化您的字符串以使 -----''' 在同一行开始以删除额外的换行符

    【讨论】:

      猜你喜欢
      • 2013-11-20
      • 2015-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-14
      • 2021-09-29
      相关资源
      最近更新 更多