1

所以我是 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
4

2 回答 2

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
于 2013-07-01T14:49:35.697 回答
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

这应该这样做

于 2013-07-01T15:28:22.373 回答