gpt4 book ai didi

python - 没有显示任何错误的刽子手游戏,但是终端没有显示任何内容

转载 作者:行者123 更新时间:2023-12-05 02:28:18 25 4
gpt4 key购买 nike

import random
from Words import words
import string

def get_valid_word(words):
word = random.choice(words)
while '-' or ' ' in word:
word = random.choice(words)
return word.upper()


def hangman() -> object:
word = get_valid_word(words)
word_letter = set(word)
alphabet = set(string.ascii_uppercase)
used_letter = set()

while len(word) > 0:
print("You have used these letter", ' '.join(used_letter))
word_list = [letter if letter in used_letter else '-' for letter in word]
print("Current word: ", ' '.join(word_list))
user_letter = input("Guess a letter: ")

if user_letter in alphabet - used_letter:
used_letter.add(user_letter)
if user_letter in word_letter:
word_letter.remove(user_letter)
elif user_letter in used_letter:
print("You have already used this letter: ")
else:
print("Invalid input")


if __name__ == '__main__':
hangman()

我无法使用代码来执行函数 hangman()。终端仅显示以下输出:

/Users/nighttwinkle/PycharmProjects/pythonProject/venv/bin/python
/Users/nighttwinkle/PycharmProjects/pythonProject/main.py

最佳答案

问题似乎出在线路上

while '-' or ' ' in word:

您的语法略有偏差,这意味着这将始终评估为 True,因此代码此时进入无限循环

python 将上面的解释为

while ('-') or (' ' in word):

因此字符串 -or 语句的一半,任何非空字符串的计算结果总是 Truewhile 条件保留为:

while True or ' ' in words:

试试这个:

while '-' in word or ' ' in word:

关于python - 没有显示任何错误的刽子手游戏,但是终端没有显示任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72699442/

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