pythonで線形探索を行っています。データには、番号と名前プロフィール情報が書かれていて、ユーザーが探したい番号の人を探し、0が入力されたら、エラーメッセージを表示させたいです。しかし、次のようなエラーが出てしまいます。対処法を教えていただきたいです。よろしくお願いいたします。
python
1class Player: 2 def __init__(self,number,name,profile,next): 3 self.number = number 4 self.name = name 5 self.profile = profile 6 self.next = next 7 8def print_info(p:Player): 9 while (p != None): 10 print("{:>2d}".format(int(p.number)),p.name,p.profile) 11 p = p.next 12 print("") 13 14def search(x,p:Player): 15 for p in data: 16 if int(p.number) ==x: 17 print("{:>2d}".format(int(p.number)),p.name,p.profile) 18 print("") 19 break 20 else: 21 print("該当する選手はいません。") 22 print("") 23 24 25 26data = [] 27 28f = open('zac_japan.txt','r',encoding = 'utf-8') 29data = f.readlines() 30f.close 31 32p=None 33for n in data: 34 new = (n.split()) 35 p = Player(int(new[0]),new[1],new[2],p) 36 37print_info(p) 38 39while True: 40 print("探索する登録番号は?") 41 x=int(input()) 42 for n in data: 43 new = (n.split()) 44 p = Player(int(new[0]),new[1],new[2],p) 45 if x == 0: 46 print("プログラムを終了します。") 47 else: 48 search(x,p) 49
エラーメッセージ
if int(p.number) ==x: AttributeError: 'str' object has no attribute 'number'
回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/06/28 09:15