2013-07-01 32 views
1

所以我是python的新手,並且在我的新pi中通過一個教程來編寫一個蟒蛇遊戲。好吧,無論如何,我喜歡代碼和所有東西,而且它運行良好,但是我想添加一些內容,讓它問:「如果你想繼續玩」,我做了大量的研究,並在一些聊天室中討論它並帶着/發現這個腳本退出:Hangman與keep_playing的遊戲故障

while keep_playing == False: 
user = raw_input("\n\tShall we play another game? [y|n] ") 
again = "yes".startswith(user.strip().lower()) 
if again: 
    keep_playing = True 
if not again: 
    break 

raw_input ("\n\n\nPress enter to exit") 

,但我得到這個錯誤:

Traceback (most recent call last): 
File "/home/pi/Desktop/Scripts/scrappy/ls/ls/hangman3.py", line 40, in <module> 
    while keep_playing == False: 
NameError: name 'keep_playing' is not defined 

時,其與該腳本運行

import random 
import urllib 

print 'time to play hangman' 
    animals = urllib.urlopen('http://davidbau.com/data/animals').read().split() 
secret = random.choice(animals) 
guesses = 'aeiou' 
turns = 5 

while turns > 0: 
missed = 0 
for letter in secret: 
    if letter in guesses: 
    print letter, 
    else: 
    print '_', 
    missed += 1 

print 

if missed == 0: 
    print 'You win!' 
    break 

guess = raw_input('guess a letter: ') 
guesses += guess 

if guess not in secret: 
    turns -= 1 
    print 'Nope.' 
    print turns, 'more turns' 
    if turns < 5: print ' O ' 
    if turns < 4: print ' \_|_/ ' 
    if turns < 3: print ' | ' 
    if turns < 2: print '/\ ' 
    if turns < 1: print ' d b ' 
    if turns == 0: 
     print 'The answer is', secret 

while keep_playing == False: 
user = raw_input("\n\tShall we play another game? [y|n] ") 
again = "yes".startswith(user.strip().lower()) 
if again: 
    keep_playing = True 
if not again: 
    break 

raw_input ("\n\n\nPress enter to exit") 

誰能幫助我? ****編輯***** 有人可以使用所提供的提示我已經解決了我的問題,這是最後的代碼

import random 
import urllib 

animals = urllib.urlopen('http://davidbau.com/data/animals').read().split() 

while True: 

print 'time to play hangman' 
secret = random.choice(animals) 
guesses = 'aeiou' 
turns = 5 

while turns > 0: 
    missed = 0 
    for letter in secret: 
     if letter in guesses: 
      print letter, 
     else: 
      print '_', 
      missed += 1 

    print 

    if missed == 0: 
     print 'You win!' 
     break 

    guess = raw_input('guess a letter: ') 
    guesses += guess 

    if guess not in secret: 
     turns -= 1 
     print 'Nope.' 
     print turns, 'more turns' 
     if turns < 5: print ' O ' 
     if turns < 4: print ' \_|_/ ' 
     if turns < 3: print ' | ' 
     if turns < 2: print '/\ ' 
     if turns < 1: print ' d b ' 
     if turns == 0: 
      print 'The answer is', secret 
      break 


user = raw_input("\n\tShall we play another game? [y|n] ") 
again = "yes".startswith(user.strip().lower()) 
if not again: 
    raw_input ("\n\n\nPress enter to exit") 
    break 

回答

1

我沒有把蟒蛇關閉此胎,但我可以看到你'確實試圖比較一個空的/未定義的變量「keep_playing」爲false。據我所知,如果你沒有在比較之前聲明變量,你不能將變量與某個變量進行比較,不過不知道Python中的變量是不同的。

,如果你寫沿其他變量這一行,會發生什麼:

keep_playing = False 

所以你會得到:

animals = urllib.urlopen('http://davidbau.com/data/animals').read().split() 
secret = random.choice(animals) 
guesses = 'aeiou' 
turns = 5 
keep_playing = False 
+0

是固定的引用錯誤,但現在它只是去「按輸入退出」,然後退出/停止使用您的提示 –

+0

我也解決了它ee編輯的文章 –

1
animals = urllib.urlopen('http://davidbau.com/data/animals').read().split() 
secret = random.choice(animals) 
guesses = 'aeiou' 
turns = 5 
keep_playing = False 

if guess not in secret: 
    turns -= 1 
    print 'Nope.' 
    print turns, 'more turns' 
    if turns < 5: print ' O ' 
    if turns < 4: print ' \_|_/ ' 
    if turns < 3: print ' | ' 
    if turns < 2: print '/\ ' 
    if turns < 1: print ' d b ' 
    if turns == 0: 
     print 'The answer is', secret 
     keep_playing = False 

這應該這樣做

+0

感謝您使用提供的提示我解決了它,請參閱編輯的文章 –