- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以我是 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/
我正在制作刽子手游戏。一切正常,我已经准备好用于使游戏失败并为猜测提供 -1 的代码。虽然将它添加到 else 语句时它会重复等于单词的长度并且它也会给出一个猜测——即使它是正确的?我看不出代码有什么
我是一名 Javascript 学生,正在开发我的 Hangman 游戏。我用于显示 secret 单词的正确破折号数量的循环不起作用。每次只显示一个破折号。感谢帮助。谢谢。 $(document).
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 关于您编写的代码问题的问题必须在问题本身中描述具体问题 — 并且包括有效代码 以重现它。参见 SSC
print("Welcome to hangman. Are you ready to have some fun?") def play(): import random List = ["ra
我刚刚开始学习 Java 作为我的第一门编程语言。 在类里面,我们被分配使用 while 和 for 循环制作一个基本的 Hangman 游戏。 到目前为止我有什么 当用户输入第一个猜测时,它确实识别
我是编程新手。我正在做一个经典的刽子手游戏,但我希望它是从节目中猜测一些角色的名字(名字和姓氏)。 我的问题:破折号打印 name 和 surname 之间的空格而不是忽略它,例如: 密语:鲁伯特·吉
我正在尝试创建一个刽子手游戏,其中每个字母都有一个按钮。我用绝对定位和多个 Action 监听器以困难的方式做到了这一点。有什么办法可以用 for 循环同时完成这两个任务吗? 另外,如何使用多态数组实
我是 Java 新手,所以这听起来可能很傻。 我正在尝试从 Java 创建一个基于 GUI 的刽子手游戏。我已经从数组创建了按钮列表,单击时它返回单个字母作为字符。我有要猜测的单词列表,每次按下按钮时
我已经研究这段代码有一段时间了。我尝试了许多不同的方法来查找玩家在随机生成的单词中正确猜测的输入的索引 - 我认为我当前编写的内容应该有效,但我担心我忽略了一个非常简单的错误。事实上,每当我运行代码时
在我的 Hangman 应用程序中,我为用户创建了一组按钮,然后从文件中随机选择 Hangman 应用程序中猜测的单词...该单词使用 JLabel 打印...但是一旦单击按钮时,应用程序会卡住。谁能
我正在为我的 C 课编程编写一个刽子手程序。我似乎不知道如何执行最后一步,即在玩家猜出所有正确的字母后停止游戏。我感觉问题出在播放功能上。 #include #include #inc
用 python 编写一个 Hangman 程序,在传入一个包含多单词字符串和单单词字符串的文件时遇到了一个问题。 文件: 你好棕色狐狸 狗 猫 水 跳跃 #initialize list wordL
所以我在玩这个游戏的特定部分时遇到了问题。这些是我的初始变量: var words = ["sugar","banana","hulk"]; var guessLeft = 12; var wrong
我是一名优秀的程序员,十分优秀!