hangman游戏尝试替换字母下划线时出错

2024-09-30 22:19:48 发布

您现在位置:Python中文网/ 问答频道 /正文

这就是我得到的错误。我仍在开发它,我只是遇到了一个阻塞点,我尝试在stackoverflow上深入研究它,同时也尝试对它进行切片。我不明白为什么它是一个项目分配,然后我循环的位置,这是一个列表。所以可能是这导致了那个部分,但我需要循环通过它,以得到什么字母位置是正确的,从而改变字符串。我被困了大约两个小时。谢谢

Traceback (most recent call last):
  File "HangMan.py", line 66, in <module>
    checkchoice(guessletter(),word)
  File "HangMan.py", line 59, in checkchoice
    board[x] = choice
TypeError: 'str' object does not support item assignment

˚

#HANGMAN GAME
import random

wordlist = ["apple", "banana", "game", "windows", "baseball"]
special_characters = "!@#$%^&*()-+?_=,<>/\"\':"
choice = ""
word = ""
hangman = ["H", "A", "N", "G", "M", "A", "N"]
wrong = 0

def randomword():
    word = random.choice(wordlist)
    return word

def createboard(word):
    letters = 0
    for x in word:
        letters += 1
    return "_" * letters

def guessletter():
    while True:
        try:
            choice = input("Choose a letter: ")
        except ValueError:
            print("Sorry that was not a valid choice.")
            continue
        if choice.isdigit():
            print("Sorry no digits allowed.")
            continue
        elif any(c in special_characters for c in choice):
            print("Sorry no special characters allowed.")
            continue
        else:
            return choice

def checkchoice(choice, word):
    count = 0
    position = []
    global wrong
    global board
    correct = False
    for x in word:
        if choice == x:
            correct = True
            position.append(count)
            pass
        else:
            pass
        count +=1
    if correct == False:
        wrong += 1
        print("Oof looks like you missed.")
        print(hangman[0:wrong])
    else:
        #ERROR HERE
        for x in position:
            board[x] = choice
        
word = randomword()
board = createboard(word)
print(board)
print(word)
while True:
    checkchoice(guessletter(),word)
    print(board)
    print(wrong)

Tags: inboardtrueforreturndefwordspecial
1条回答
网友
1楼 · 发布于 2024-09-30 22:19:48

我终于找到了一个成功的例子

尽管看起来很奇怪,但它只在Java GUI应用程序的上下文中工作

因此,简单地扩展JFrame并在单独的线程上启动读卡器捕获似乎就足够了

在我能看到的SDK文档中,这个要求没有具体说明

更新

问题似乎比我最初想象的更严重。不仅必须在Java GUI应用程序的上下文中调用API,而且GUI还必须处于焦点位置,否则捕获调用根本不会返回

我已经用示例SDK应用程序验证了这一点。如果应用程序未处于焦点,则Capture()方法不会返回。这也适用于C#示例,其中窗口必须处于焦点位置,这表明这是内置于解决方案附带的DLL中的

这对于我们的场景来说很糟糕,我们想要开发一个浏览器可以与之通信的本地服务,因为当浏览器处于焦点时,Java应用程序显然不是

相关问题 更多 >