gpt4 book ai didi

python - keep_playing 的刽子手游戏出现问题

转载 作者:行者123 更新时间:2023-12-01 05:41:33 24 4
gpt4 key购买 nike

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

最佳答案

我不喜欢 python,但我可以看到你确实在尝试将空/ undefined variable “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

关于python - keep_playing 的刽子手游戏出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17407206/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com