[英]'int' object is not subscriptable error
I am a beginner at python, I am trying to make a program that quizzes you on what note is on what fret of the guitar. 我是python的初学者,我正在尝试制作一个程序来询问您有关吉他哪种音调的问题。
I've changed my code up some, but now the error says - 我已经更改了一些代码,但是现在错误提示-
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()
How can I fix it now? 我现在该如何解决?
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!")
Thanks to everyone who is helping me out with this. 感谢所有在此方面帮助我的人。
You don't have other_string
marked as global in guessing, so when you assign it to a string in guessing()
it is creating a new variable other_string
that is just in the scope of the guessing()
function. 您没有在
other_string
标记为全局字符串,因此当您在other_string
guessing()
中将其分配给字符串时,它将创建一个新变量other_string
,该变量仅在other_string
guessing()
函数的范围内。 To fix this, add global other_string
to the top of guesing()
to make your reassignment of it within guessing()
visible at the global scope. 为了解决这个问题,加上
global other_string
到顶部guesing()
使在您的这再分配guessing()
在全球范围内可见。 Without this, other_string
is still seen as an int in the global namespace, and ints cannot be subscripted . 没有这个,
other_string
在全局名称空间中仍被视为int,并且ints无法下标。
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}
When initilize other_string
, use other_string = {}
other than other_string = 0
, this will tell you other_string
is a dictionary, and you should only use it as dictionary
other than any other data structures; 当
other_string
,使用other_string = {}
other_string = 0
之外的other_string = {}
,这将告诉您other_string
是一个字典,您只能将它用作其他数据结构以外的dictionary
; the other_string[note]
is an int
value. other_string[note]
是一个int
值。
declare other_string as global
in your guessing()
method, then you can modify it in the body of guessing()
声明other_string作为
global
在你guessing()
方法,那么您可以在体内修改它guessing()
print(guessing())
will not print anything, you did not return any value in your guessing()
method, you should add return guess
at the end, and it should be aligned with the outer if-elif statement. print(guessing())
将不会打印任何内容,您没有在guessing()
方法中返回任何值,您应该在末尾添加return guess
,并且应与外部if-elif语句对齐。
other_string
is not an iterable object. other_string
不是可迭代的对象。 You might initialize it as other_string=[]
您可以将其初始化为
other_string=[]
Although others have identified the specific error, I thought you might be interested to see an alternative implementation addressing the concerns I raised in my comment: 尽管其他人已经确定了具体错误,但我认为您可能有兴趣看到替代解决方案,以解决我在评论中提出的问题:
import random
class Guitar:
NAMES = {1: "first", 2: "second", 3: "third",
4: "fourth", 5: "fifth", 6: "sixth"}
STRINGS = {1: "E", 2: "B", 3: "G", 4: "D", 5: "A", 6: "E"}
NOTES = ["A", "A#", "B", "C", "C#", "D",
"D#", "E", "F", "F#", "G", "G#"] * 2
FRETS = 12
def play(self, guesses=8):
while True:
question = random.randrange(2)
self.string = random.choice(self.STRINGS.keys())
if question:
answer = random.choice(self.NOTES)
template = "On the {0} string, which fret is {1}? "
valid_input = self.valid_fret
checker = self.note_at_fret
else:
answer = random.randint(0, self.FRETS)
template = "On the {0} string, which note is fret {1}? "
valid_input = self.valid_note
checker = self.fret_of_note
prompt = template.format(self.NAMES[self.string], answer)
for _ in range(guesses):
if checker(valid_input(prompt)) == answer:
print("Correct!\n")
break
print("Incorrect.")
else:
print("Out of guesses.\n")
def valid_fret(self, prompt):
while True:
fret = input(prompt)
try:
fret = int(fret)
except ValueError:
print("Not a valid fret.")
else:
if fret in range(self.FRETS+1):
return fret
print("Not a valid fret.")
def valid_note(self, prompt):
while True:
note = input(prompt).upper()
if note in self.NOTES:
return note
print("Not a valid note.")
def note_at_fret(self, fret):
return self.string_notes()[fret]
def fret_of_note(self, note):
return self.string_notes().index(note)
def string_notes(self):
open_string = self.STRINGS[self.string]
return self.NOTES[self.NOTES.index(open_string):]
if __name__ == "__main__":
Guitar().play()
Note: 注意:
global
variables; global
变量。 str.format
and assignment of methods (eg self.valid_note
) to local names (eg valid_input
) have been used to reduce duplication; str.format
和将方法(例如self.valid_note
)分配给本地名称(例如valid_input
)可减少重复; string1
, string2
is bad practice that suggests that a list should have been used); string1
, string2
是不良做法,建议应使用列表); and You can even use sub-classing to easily test yourself on non-standard tunings: 您甚至可以使用子类轻松地对非标准调优进行测试:
class OpenG(Guitar):
STRINGS = {1: "D", 2: "B", 3: "G", 4: "D", 5: "G", 6: "D"}
OpenG().play()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.