python - 用 Python 编写程序以显示在刽子手游戏中猜到的字母不起作用
问题描述
我必须用 Python 编写一个关于玩游戏刽子手的代码。目前我的代码无法用猜测的字母正确替换“_”,它只替换一个并且不显示所有猜测的字母。
我的代码如下:
import random
word = random.choice(["bread", "clouds", "plane"])
guess = 0
correct_guess = 0
play_again = True
print(word)
name = input("Hello, what's your name? ")
print('Hello', name)
status = input('Do you wish to play hangman? Yes/No ')
hyphens = print('_ ' * (len(word)))
if status == 'No' 'no' 'N' 'n':
exit()
while play_again is True:
letter = input('Guess a letter: ')
if letter in word:
guess = guess + 1
correct_guess = correct_guess + 1
print('You guessed a letter correctly!')
position = word.find(letter)
print("The letter is in position", position + 1, "out of", len(word), "in the word. Guess Count:", guess)
if position == 0:
print(letter, "_ _ _ _")
if position == 1:
print("_", letter, "_ _ _ ")
if position == 2:
print("_ _", letter, "_ _ ")
if position == 3:
print("_ _ _", letter, "_ ")
if position == 4:
print("_ _ _ _", letter)
else:
guess = guess + 1
print("That letter isn't in the word. Guess Count:", guess)
if guess == 10:
print('Bad luck. You lost.')
play = input('Do you want to play again? ')
if play == 'No' 'no' 'N' 'n':
exit()
if correct_guess == 5:
print('Congratulations! You have guessed the word! The word was', word + '.')
play = input('Do you want to play again? ')
if play == 'No' 'no' 'N' 'n':
exit()
非常感激您的帮忙。另外,我对编程知之甚少,而且我对它还很陌生。
詹姆士
解决方案
你不能写这样的表达式:
if status == 'No' 'no' 'N' 'n':
正确的方法是:
if play == 'No' or play == 'no' or play == 'N' or play == 'n':
或者:
if play in ['No' ,'no', 'N' ,'n']
一种解决方案:
import random
word = random.choice(["bread", "clouds", "plane"])
print(word)
name = input("Hello, what's your name? ")
print('Hello', name)
status = input('Do you wish to play hangman? Yes/No ')
def play():
if status == 'No' or status == 'no' or status == 'N' or status == 'n':
print ("Goodbye")
return # exit
else:
print('_ ' * (len(word))) # _ _ _ _ _
guess = 0
correct_guess = 0
play_again = True
pos_list = ['_' for x in range(len(word))] # define list where you will put your guessed letters ['_', '_', '_', '_', '_']
while play_again is True:
letter = input('Guess a letter: ')
if letter in word:
guess = guess + 1
correct_guess = correct_guess + 1
print('You guessed a letter correctly!')
position = word.find(letter)
print("The letter is in position", position + 1, "out of", len(word), "in the word. Guess Count:", guess)
pos_list[position] = letter # save letter at position # ['_', '_', '_', 'a', '_']
print (' '.join(pos_list)) # _ _ _ a _
else:
guess = guess + 1
print("That letter isn't in the word. Guess Count:", guess)
if guess == 10:
print('Bad luck. You lost.')
play = input('Do you want to play again? ')
if play == 'No' or play == 'no' or play == 'N' or play == 'n':
print("Goodbye")
return
else:
print('_ ' * (len(word)))
if correct_guess == len(word):
print('Congratulations! You have guessed the word! The word was', word + '.')
play = input('Do you want to play again? ')
if play == 'No' or play == 'no' or play == 'N' or play == 'n':
print("Goodbye")
return
else:
print('_ ' * (len(word)))
play()
推荐阅读
- unity3d - 分区域分屏控制对象
- algorithm - 如何根据另一个地图的键值对 Go 中的地图进行排序?
- html - 溢出-y:当超过最大高度的内容可见时滚动创建滚动条:隐藏
- linux - 为什么我的 camphish hackingtool 图片没有加载?
- unity3d - 使用 DOTween 的鼠标单击动画淡出不起作用
- python - 当它从 10 年到 510 年时,我如何减速
- openapi - 如何使用 Microprofile Openapi swagger-ui 中的注释传递 JWT 令牌
- java - 采访 Q:从字符串“我正在为亚马逊工作”中反转一个单词“亚马逊”
- gravity-forms-plugin - Gravityview - 显示另一个表单的付款状态
- node.js - 无法安装 nodejs - 应用安全设置时出错