' INT'对象不是可下载的错误

时间:2014-07-09 15:31:53

标签: 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()

我现在该如何解决?

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!")

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

4 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

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. 在初始化other_string时,使用other_string = {}以外的other_string = 0,这会告诉您other_string是字典,您只能将其用作{{} 1}}除了任何其他数据结构; dictionaryother_string[note]值。

  2. int方法中将other_string声明为global,然后您可以在guessing()

  3. 的主体中对其进行修改
  4. guessing()不会打印任何内容,您在print(guessing())方法中没有返回任何值,您应该在最后添加guessing(),并且应该与外部if-elif陈述。

答案 2 :(得分:0)

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

答案 3 :(得分:0)

虽然其他人已经确定了具体错误,但我认为您可能有兴趣看到我在评论中提出的替代实施方案:

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

注意:

  1. 已定义一个类来保存所需的状态,删除global个变量;
  2. 将“神奇数字”8分解为易于理解的论点;
  3. 字典,str.format以及方法(例如self.valid_note)与本地名称(例如valid_input)的分配已被用于减少重复;
  4. 单独的功能已将程序分解为更小,更易于理解的“功能块”;
  5. 已添加输入验证以防止用户崩溃程序;
  6. 变量名称中的最小数据(例如string1string2是不好的做法,表明应该使用列表);和
  7. 对所选的音符/音品没有限制 - 全部自动处理。
  8. 您甚至可以使用子分类轻松地在非标准调音中测试自己:

    class OpenG(Guitar):
        STRINGS = {1: "D", 2: "B", 3: "G", 4: "D", 5: "G", 6: "D"}
    
    
    OpenG().play()