所以我是python的新手,并通过一个教程在我的新pi上用Python 2.7编写一个刽子手游戏。好吧,无论如何我喜欢代码和一切,它运行良好,但后来我想添加一些东西让它问,“如果你想继续玩”,我做了很多研究,并在一些聊天室谈论它并提出/找到这个脚本退出:
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
答案 0 :(得分:1)
我没有进入python,但我可以看到你确实试图将空/未定义变量“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
答案 1 :(得分: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
这应该这样做