以下のプログラムで、以下のようなエラーが出ました。
'NoneType' object is not iterable
調べましたが、どこがいけないかの特定ができません。
型の違う変数どうしを代入しようとしているわけでもありません。
このプログラムは最適化問題の一つで、
合計300円以内のお菓子の選定において、
最も満足度が高い組み合わせを選出するものです。
ソースコードは教科書の丸写しです。
Python
1# -*- coding: utf-8 -*- 2""" 3Created on Thu Dec 17 05:04:11 2020 4 5@author: motch 6""" 7 8#10-2 9class Snack: 10 def __init__(self,name,price,like): 11 self.name=name 12 self.price=price 13 self.like=like 14 15def total_price(sn_list): 16 total=0 17 for x in sn_list: 18 total+=x.price 19 return total 20 21def total_like(sn_list): 22 total=0 23 for x in sn_list: 24 total+=x.like 25 return total 26 27def print_snack_list(sn_list): 28 for x in sn_list: 29 print(x.name) 30 print("合計:"+str(total_price(sn_list))) 31 print("満足度:"+str(total_like(sn_list))) 32 33def best_list(l,sn_set): 34 max_like=0 35 bestl=l 36 for s in sn_set: 37 if total_price(l+[s])>300: 38 continue 39 rl=best_list(l+[s],sn_set) 40 if max_like<total_like(rl): 41 max_like=total_like(rl) 42 bestl=rl 43 return bestl 44 45sn1=Snack("チョコ",130,18) 46sn2=Snack("ポテチ",120,15) 47sn3=Snack("クッキー",80,12) 48sn4=Snack("ラムネ",30,4) 49sn5=Snack("ガム",20,2) 50sn_set={sn1,sn2,sn3,sn4,sn5} 51print_snack_list(best_list([],sn_set)) 52 53 54
エラーメッセージ
runfile('C:/Users/motch/.spyder-py3/10-2.py', wdir='C:/Users/motch/.spyder-py3')
Traceback (most recent call last):
File "<ipython-input-12-b11e1e9d4a81>", line 1, in <module>
runfile('C:/Users/motch/.spyder-py3/10-2.py', wdir='C:/Users/motch/.spyder-py3')
File "C:\Users\motch\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 827, in runfile
execfile(filename, namespace)
File "C:\Users\motch\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "C:/Users/motch/.spyder-py3/10-2.py", line 51, in <module>
print_snack_list(best_list([],sn_set))
File "C:/Users/motch/.spyder-py3/10-2.py", line 39, in best_list
rl=best_list(l+[s],sn_set)
File "C:/Users/motch/.spyder-py3/10-2.py", line 39, in best_list
rl=best_list(l+[s],sn_set)
File "C:/Users/motch/.spyder-py3/10-2.py", line 39, in best_list
rl=best_list(l+[s],sn_set)
File "C:/Users/motch/.spyder-py3/10-2.py", line 39, in best_list
rl=best_list(l+[s],sn_set)
File "C:/Users/motch/.spyder-py3/10-2.py", line 39, in best_list
rl=best_list(l+[s],sn_set)
File "C:/Users/motch/.spyder-py3/10-2.py", line 40, in best_list
if max_like<total_like(rl):
File "C:/Users/motch/.spyder-py3/10-2.py", line 23, in total_like
for x in sn_list:
TypeError: 'NoneType' object is not iterable
runfile('C:/Users/motch/.spyder-py3/10-2.py', wdir='C:/Users/motch/.spyder-py3')
Traceback (most recent call last):
File "<ipython-input-13-b11e1e9d4a81>", line 1, in <module>
runfile('C:/Users/motch/.spyder-py3/10-2.py', wdir='C:/Users/motch/.spyder-py3')
File "C:\Users\motch\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 827, in runfile
execfile(filename, namespace)
File "C:\Users\motch\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "C:/Users/motch/.spyder-py3/10-2.py", line 51, in <module>
print_snack_list(best_list([],sn_set))
File "C:/Users/motch/.spyder-py3/10-2.py", line 39, in best_list
rl=best_list(l+[s],sn_set)
File "C:/Users/motch/.spyder-py3/10-2.py", line 39, in best_list
rl=best_list(l+[s],sn_set)
File "C:/Users/motch/.spyder-py3/10-2.py", line 39, in best_list
rl=best_list(l+[s],sn_set)
File "C:/Users/motch/.spyder-py3/10-2.py", line 39, in best_list
rl=best_list(l+[s],sn_set)
File "C:/Users/motch/.spyder-py3/10-2.py", line 39, in best_list
rl=best_list(l+[s],sn_set)
File "C:/Users/motch/.spyder-py3/10-2.py", line 40, in best_list
if max_like<total_like(rl):
File "C:/Users/motch/.spyder-py3/10-2.py", line 23, in total_like
for x in sn_list:
TypeError: 'NoneType' object is not iterable
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/12/16 23:39