我正在嘗試使用 Python 創建一個劊子手游戲。雖然,當“太陽”一詞被選中時,如果“樹”這個詞被輸入到我的答案中,它就說答案是正確的。我曾嘗試創建一個函式來解決這種情況,但是,它對我不起作用......
這是我的代碼:
#hangman mini-project
import random
import string
import time
letters = string.ascii_letters
lettertree = ['a', 'b', 'c', 'd', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 's', 'u', 'v', 'w', 'x', 'y', 'z']
hangmanwords = ['tree','sun']
sunchoices = ['s _ _', '_ u _', '_ _ n']
treechoices = ['t _ _ _', '_ r _ _', ' _ _ e _', '_ _ _ e']
lettercount = [0]
gameWinTree = False
gameWinSun = False
limbCount = 5
hangmanword = random.choice(hangmanwords)
correct = hangmanword
if hangmanword == "sun":
print (random.choice(sunchoices))
if hangmanword == "tree":
print (random.choice(treechoices))
if letters == "r":
print("Nice! You guessed one letter from the word")
if letters == "e":
print("Wow! You guessed two letters from the word, if you wish, you can guess the word")
while True:
letters = input("Please enter a letter to guess the word")
if letters == "tree":
input("Correct! The word was tree! Press enter to play again.")
time.sleep(1)
break
if letters == "tree":
gameWinTree == true
if gameWinTree == true:
time.sleep(1)
break
print("The letter that you chose was " letters)
if letters == "r":
print("Nice! You guessed one letter from the word!\n t r _ _")
if letters == "e":
print("Wow! You guessed two letters from the word!\n t _ e e")
if letters == "tree":
print("Correct! The word was tree!")
if letters == lettertree:
print("Sorry, that's not a correct letter, feel free to try again.")
limbCount -=1
if limbCount == 0:
print("Unfortunately, you are out of tries, better luck next time!")
time.sleep(1)
exit()
基本上,如果選擇“sun”這個詞,我希望這個詞樹的代碼不起作用。如果我的代碼草率,也很抱歉,嘗試快速創建它!謝謝
uj5u.com熱心網友回復:
專門針對您的問題
在您當前的代碼中,您正在檢查用戶是否猜到了“樹”,而您實際上只想檢查所選單詞是否為“樹”。因此,在您的 if 陳述句中,您可以添加一個“和”,并額外檢查正確的單詞。
if letters == "tree" and correct == "tree":
更籠統
正如已經指出的那樣,無論選擇哪個詞作為正確的詞,在您的游戲回圈中,您總是在尋找猜測“樹”。
你已經用正確的單詞保存了一個變數來檢查,所以使用它而不是檢查“樹”:
if letters == "tree":
應該是:
if letters == correct:
然后讓代碼撰寫正確的訊息連接列印中的字串:
input("Correct! The word was " correct " Press enter to play again.")
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/333194.html
上一篇:洗掉非阿爾法、編輯、添加非阿爾法
