2013-08-01 53 views
1

我需要用python程序編寫一個hang子手程序,但是我不知道如何繼續。 在程序 我被給予6次機會,否則被稱爲「生命線」,猜測字中的字母。每一次錯誤的猜測都會縮短「生命線」。當你正確猜測這個單詞或者你已經用完了所有的「生命線」時,遊戲就會結束。這裏是示例輸出:用蟒蛇編程的Hang子手程序

['_', '_', '_', '_'] 

***Life Line*** 
-+-+-+-+-+-+ 
$|$|$|$|$|$| 
-+-+-+-+-+-+ 

Enter your guess: a 
['_', '_', '_', '_'] 

***Life Line*** 
-+-+-+-+-+ 
$|$|$|$|$| 
-+-+-+-+-+ 

Incorrect Guess: ['a'] 
................................... 
Enter your guess: b 
['b', '_', '_', '_'] 

***Life Line*** 
-+-+-+-+-+ 
$|$|$|$|$| 
-+-+-+-+-+ 

Incorrect Guess: ['a'] 
................................... 
Enter your guess: l 
['b', 'l', '_', '_'] 

***Life Line*** 
-+-+-+-+-+ 
$|$|$|$|$| 
-+-+-+-+-+ 

Incorrect Guess: ['a'] 
................................... 
Enter your guess: o 
['b', 'l', '_', '_'] 

***Life Line*** 
-+-+-+-+ 
$|$|$|$| 
-+-+-+-+ 

Incorrect Guess: ['a', 'o'] 
................................... 
Enter your guess: u 
['b', 'l', 'u', '_'] 

***Life Line*** 
-+-+-+-+ 
$|$|$|$| 
-+-+-+-+ 

Incorrect Guess: ['a', 'o'] 
................................... 
Enter your guess: e 
['b', 'l', 'u', 'e'] 

***Life Line*** 
-+-+-+-+ 
$|$|$|$| 
-+-+-+-+ 

Incorrect Guess: ['a', 'o'] 
................................... 
You Got it Right! Well Done! 

我已經鍵入了前幾個代碼,但卡住了。

import random 
wordList = ["Mary","Tian Pei","Pong"] 
randname = random.choice (wordList) 
print randname 

resultList = [ ] 

for i in range(len(randname)): 
    resultList.append("_") 
print resultList 
+2

你卡在了什麼?你有什麼嘗試,沒有奏效?你的問題是什麼? –

+1

嘗試編寫一個打印出生命的例程,基於傳遞給它的整數。將程序分解成各個部分,並讓每個人自行工作。 – Jiminion

+0

我不知道如何消除「_」,並將信件添加到它以及如何消除LifeLines。提前感謝您的關注。 –

回答

2

創建空白列表:

>>> name = "Mary" 
>>> blanks = ["_" for letter in name] 
>>> blanks 
['_', '_', '_', '_'] 

創建不正確的猜測的名單:

>>> incorrect_guesses = # you figure this one out 

設置你的生命線:

>>> life_lines = # you figure this one out 

提示輸入猜測:

>>> guess = raw_input("Guess: ") 
Guess: a 
>>> guess 
'a' 

保存變量,它說,猜測是否是不正確的:

>>> incorrect = # you figure this one out 

遍歷name和字母替換相應的空行blanks如果name各個字母是一樣的作爲guess

>>> for i in range(len(name)): 
...  if name[i] == guess: 
...   incorrect = # you figure this one out 
...   blanks[i] = # you figure this one out 
... 
>>> blanks 
['_', 'a', '_', '_'] 

如果incorrectTrue,加guessincorrect_guesses(在這種情況下,由於猜測是正確的,它不會更新),並減去生命線:

>>> if incorrect: 
...  incorrect_guesses.append(# you figure this one out) 
...  life_lines -= # you figure this one out 
... 

要檢查是否等價,加入字母blanks重新形成原詞,這樣就可以比較兩個:

>>> final = ''.join(blanks) # this connects each letter with an empty string 
>>> final 
'_a__' 

一般控制結構可能是以下幾點:

choose random word 
create blanks list 
set up life-lines 
while life_lines is greater than 0 and word not completed: 
    print blanks list 
    print life_lines graphic 
    if there are incorrect guesses: 
     print incorrect guesses 
    prompt for guess 
    check if correct and fill in blanks 
    if incorrect: 
     add to incorrect guesses and subtract a life-line 
if word completed: 
    you win 
else: 
    you lose 

它是由你來填寫我在這裏寫的空白(並編寫自己的例程以打印生活線圖形等)。

+0

非常感謝您的幫助。 –

+0

@SeanNg請考慮接受我們的答案之一,如果您的問題是由其中一方或雙方解決的話。 – 2rs2ts

0

你可以這樣做。有關邏輯的解釋,請參閱代碼中的註釋。

#!/usr/bin/env python 
import random 

wordList = ["Mary","Tian Pei","Pong"] 
randname = random.choice (wordList) 

resultList = [] 

# When creating the resultList you will want the spaces already filled in 
# You probably don't want the user to guess for spaces, only letters. 
for letter in randname: 
    if letter == ' ': 
     resultList.append(' ') 
    else: 
     resultList.append("_") 

# We are storing the number of lifeline 'chances' we have here 
# and the letters that have already been guessed 
lifeline = 6 
guesses = [] 


# The game does not end until you win, on run out of guesses 
while guesses != 0: 

    # This prints the 'scoreboard' 
    print resultList 
    print '***Life Line***' 
    print '-+' * lifeline 
    print '$|' * lifeline 
    print '-+' * lifeline 
    print 'Incorrect Guess: %s' % guesses 

    # If the win condition is met, then we break out of the while loop 
    # I placed it here so that you see the scoreboard once more before 
    # the game ends 
    if ''.join(resultList) == randname: 
     print 'You win!' 
     break 

    # raw_input because I'm using 2.7 
    g = raw_input("What letter do you want to guess?:") 

    # If the user has already guessed the letter incorrectly, 
    # we want to forgive them and skip this iteration 
    if g in guesses: 
     print 'You already guessed that!' 
     continue 

    # The lower port here is important, without it guesses are 
    # case sensitive, which seems wrong. 
    if g.lower() in [letter.lower() for letter in randname]: 
     # this goes over each letter in randname, then flips 
     # the appropriate postions in resultList 
     for pos, letter in enumerate(randname): 
      if g.lower() == letter.lower(): 
       resultList[pos] = letter 
    # If the letter is not in randname, we reduce lifeline 
    # add the guess to guesses and move on 
    else: 
     lifeline -= 1 
     guesses.append(g) 

讓我知道你是否需要任何澄清。

+0

非常感謝您的幫助。 –