gpt4 book ai didi

python - “int”对象不可下标错误

转载 作者:行者123 更新时间:2023-12-01 05:08:24 25 4
gpt4 key购买 nike

我是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()

我现在该如何修复它?

import random
import sys

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}

string = random.randint(1,6)
question_type = random.randint(1,2)
correct = 0
notes = []
guess = 0
note = 0
other_string = {}
guessed = []
def guessing (string, question_type, correct, notes, guess, note, other_string, guessed):
if question_type == 1:
if string == 1:
other_string = string1
for key,value in string1.items():
notes.append(key)
note = random.choice(notes not in guessed)
guess = input("On the first string, where is the " + note + " note?")

elif string == 2:
other_string = string2
for key,value in string2.items():
notes.append(key)
note = random.choice(notes not in guessed)
guess = input("On the second string, where is the " + note + " note?")

elif string == 3:
other_string = string3
for key,value in string3.items():
notes.append(key)
note = random.choice(notes not in guessed)
guess = input("On the third string, where is the " + note + " note?")

elif string == 4:
other_string = string4
for key,value in string4.items():
notes.append(key)
note = random.choice(notes not in guessed)
guess = input("On the fourth string, where is the " + note + " note?")

elif string == 5:
other_string = string5
for key,value in string5.items():
notes.append(key)
note = random.choice(notes not in guessed)
guess = input("On the fifth string, where is the " + note + " note?")

elif string == 6:
other_string = string6
for key,value in string6.items():
notes.append(key)
note = random.choice(notes not in guessed)
guess = input("On the sixth string, where is the " + note + " note?")
elif question_type == 2:
if string == 1:
other_string = string1
for key,value in string1.items():
notes.append(value)
note = random.choice(notes not in guessed)
guess = input("On the first string, what note is at the " + str(note) + " fret?")

elif string == 2:
other_string = string2
for key,value in string2.items():
notes.append(value)
note = random.choice(notes not in guessed)
guess = input("On the second string, what note is at the " + str(note) + " fret?")

elif string == 3:
other_string = string3
for key,value in string3.items():
notes.append(value)
note = random.choice(notes not in guessed)
guess = input("On the third string, what note is at the " + str(note) + " fret?")

elif string == 4:
other_string = string4
for key,value in string4.items():
notes.append(value)
note = random.choice(notes not in guessed)
guess = input("On the fourth string, what note is at the " + str(note) + " fret?")

elif string == 5:
other_string = string5
for key,value in string5.items():
notes.append(value)
note = random.choice(notes not in guessed)
guess = input("On the fifth string, what note is at the " + str(note) + " fret?")

elif string == 6:
other_string = string6
for key,value in string6.items():
notes.append(value)
note = random.choice(notes not in guessed)
guess = input("On the sixth string, what note is at the " + str(note) + " fret?")
guessed.append(note)
return guess
while correct < 8:
return guessing(string, question_type, correct, notes, guess, note, other_string, guessed)
if guess == other_string[note]:
print("Good Job.")
correct +=1
else:
print("Not quite")
return guessing()
else:
print ("Good Job! You got all of the questions right!")

感谢所有帮助我解决这个问题的人。

最佳答案

您没有在猜测中将 other_string 标记为全局,因此当您将其分配给 guessing() 中的字符串时,它会创建一个新变量 other_string 恰好在 guessing() 函数的范围内。要解决此问题,请将 global other_string 添加到 guesing() 的顶部,以使您在 guessing() 中对其进行的重新分配在全局范围内可见。如果没有这个,other_string 在全局命名空间中仍然被视为 int,并且 int 不能有下标。

关于python - “int”对象不可下标错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24657851/

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