我正试图设计一个简单的刽子手游戏,目前有问题与循环这个游戏。我对Python非常陌生,并且知道这可能有一个非常简单的问题,但是非常感谢您的帮助。你知道吗
以下是我目前拥有的游戏代码:
import random
random_words = ["stationery", "notepad", "pencil", "paper","eraser","highlighter","stapler","sharpener"]
computer_choice = random.choice(random_words)
print("The number of letters in the word I have chosen is " + str(len(computer_choice) + ".")
player_guess = None
guessed_letters = []
word_guessed = []
for letter in computer_choice:
word_guessed.append("-")
joined_word = None
player_guess = str(input("Please pick a letter you think is in the word I have chosen."))
attempts = (len(computer_choice)-1)
for letter in (0, len(computer_choice)):
if attempts != 0 and "-" in word_guessed:
joined_word = "".join(word_guessed)
print(joined_word)
guessed_letters.append(player_guess)
for letter in range(len(computer_choice)):
if player_guess == computer_choice[letter]:
word_guessed[letter] = user_input
if player_guess not in computer_choice:
attempts -= 1
player_guess = str("Please try again. You have " + str(attempts) + " attempts remaining.")
if "-" not in word_guessed:
print("Congratulations! {} was the word").format(computer_choice)
else:
print("Unlucky! The word was " + str(computer_choice) + "!")
目前,游戏没有循环,只是直接切到了‘倒霉,被人’。我该怎么解决这个问题?问题是什么?你知道吗
当您发布代码时,请识别它,这样更容易理解,并且python可以编译,因为这在解释器中是非常重要的。你知道吗
您的代码在循环中出现了一些错误,如不要使用for循环循环遍历简单的重复,使用for循环遍历列表。简单的重复使用。同样的变量在两个嵌套for循环中,使事情变得复杂。你知道吗
同样不需要循环遍历另一个for循环中的每个字母,
in
操作符将检查word中是否存在字符。你知道吗我创建了另一种方法来查找和替换单词中的字符,我想更简单一点
相关问题 更多 >
编程相关推荐