如何在-while循环中打印字符串的单个副本?

时间:2017-11-09 06:06:47

标签: python

刚刚开始python,最后得到-while-loops

*转到if-else的,函数和for / while循环

如何让它自己打印?

def hangman(secret):
    tries = 5
    word = ''
    while tries > 0:
        guess = raw_input("Guess a letter: ")
        for index in secret:
            common = 0
            for char in guess:
                if char in index:
                    common = 1 """checks whether they have something in 
                                  commmon"""
            if common == 1 or index in " .?!": """checks if the secret_word 
                                                 has the 4 characters"""
                word = word + index """word is now the common letters"""
            else:
                word = word + '-' """letters not in common are a dash"""
        tries -= 1
        print 'You have', tries, 'tries left.'
        print word


>>> hangman('hello')
Guess a letter: h
You have 4 tries left.
h----
Guess a letter: e
You have 3 tries left.
h-----e---
Guess a letter: l
You have 2 tries left.
h-----e-----ll-
Guess a letter: o
You have 1 tries left.
h-----e-----ll-----o
Guess a letter: a
You have 0 tries left.
h-----e-----ll-----o-----

p.s我不确定我是否正确地完成了代码所有我知道的是它运行。     return结束循环

1 个答案:

答案 0 :(得分:0)

删除包含' - '

的else条件