首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >keep_playing的绞刑者游戏麻烦

keep_playing的绞刑者游戏麻烦
EN

Stack Overflow用户
提问于 2013-07-01 22:42:48
回答 2查看 147关注 0票数 1

因此,我是python的新手,通过一个教程,在我的新pi上用python2.7编写了一个绞刑者游戏。好吧,不管怎么说,我喜欢代码和所有的东西,它运行得很好,但是我想添加一些东西,让它问,“如果你想继续玩”,我做了很多研究,在一些聊天室里谈到了它,并提出/找到了这个脚本来退出:

代码语言:javascript
运行
复制
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")

但是我得到了这个错误:

代码语言:javascript
运行
复制
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

当它与此脚本一起运行时

代码语言:javascript
运行
复制
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")

有人可以帮助我吗?*编辑*有人可以使用提示关闭这个踏步,只要我已经解决了我的问题这是最终的代码

代码语言:javascript
运行
复制
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
EN

回答 2

Stack Overflow用户

发布于 2013-07-01 22:49:36

我对python不感兴趣,但我可以看出您确实在尝试将一个空的/未定义的变量"keep_playing“与false进行比较。据我所知,不能将一个变量与某个东西进行比较,如果在比较之前没有声明变量,则不能确定这在Python中是否不同。

如果沿着其他变量写下这一行,会发生什么:

代码语言:javascript
运行
复制
keep_playing = False

所以你会得到:

代码语言:javascript
运行
复制
animals = urllib.urlopen('http://davidbau.com/data/animals').read().split()
secret = random.choice(animals)
guesses = 'aeiou'
turns = 5
keep_playing = False
票数 1
EN

Stack Overflow用户

发布于 2013-07-01 23:28:22

代码语言:javascript
运行
复制
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

这应该就行了

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17407206

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档