“int”对象不是subscriptab

2024-09-28 23:53:13 发布

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

我是python的初学者,我正在尝试制作一个程序,询问你吉他的哪个音符是什么。在

我已经修改了一些代码,但是现在错误显示-

Traceback (most recent call last):
  File "C:\Users\Jesse\Documents\Documents\Games I have created\Python games\Guitar Memorization.py", line 107, in <module>
    print (guessing(string, question_type, correct, notes, guess, note, other_string, guessed))
  File "C:\Users\Jesse\Documents\Documents\Games I have created\Python games\Guitar Memorization.py", line 46, in guessing
    note = random.choice(notes not in guessed)
  File "C:\Python33\lib\random.py", line 248, in choice
    i = self._randbelow(len(seq))
TypeError: object of type 'bool' has no len()

我现在怎么能修好它?在

^{pr2}$

感谢所有帮助我的人。在


Tags: inpystringhavelineusersgamesdocuments
3条回答

您在猜测中没有将other_string标记为global,所以当您将它赋给guessing()中的字符串时,它正在创建一个新的变量other_string,它正好在guessing()函数的作用域内。要解决此问题,请将global other_string添加到guesing()的顶部,使您在{}内的重新分配在全局范围内可见。如果不这样,other_string在全局命名空间中仍被视为int,并且不能订阅int。在

other_string不是iterable对象。您可以将其初始化为other_string=[]

string1 = {"E":0,"F":1,"G":3,"A":5,"B":7,"C":8,"D":10,"E":12}
string2 = {"A":0,"B":2,"C":3,"D":5,"E":7,"F":8,"G":10,"A":12}
string3 = {"D":0,"E":2,"F":3,"G":5,"A":7,"B":9,"C":10,"D":12}
string4 = {"G":0,"A":2,"B":4,"C":5,"D":7,"E":9,"F":10,"G":12}
string5 = {"B":0,"C":1,"D":3,"E":5,"F":6,"G":8,"A":10,"B":12}
string6 = {"E":0,"F":1,"G":3,"A":5,"B":7,"C":8,"D":10,"E":12}
  1. 除此之外,{{{cd2>以外的任何一个{cd2>都应该使用

  2. 在您的guessing()方法中将其他的字符串声明为global,然后您可以在guessing()的主体中修改它

  3. print(guessing())不会打印任何内容,您没有在您的guessing()方法中返回任何值,您应该在末尾添加return guess,并且它应该与外部if elif语句对齐。

相关问题 更多 >