1
def temperature(weather):
'''(list of ints) -> list of strs
Modify and return list, replacing each temp in list for weather condition. 
Hot being over 25 degrees, and cool being under.
'''

したがって、temperature([24, 29, 11]) を実行すると、['cool', 'hot', 'cool'] が返されます。

これは私が得たものです。ただし、リストを変更するのではなく、新しいリストを作成していると思います。for ループを使用して新しいリストを作成する代わりに、リストを変更するにはどうすればよいですか?

temp =[]
for degrees in weather:
    if degrees > 25:
        temp = temp + ['hot']
    else:
        temp = temp + ['cool']
return temp
4

3 に答える 3

1

渡された引数を変更しないでください。

temp = []
 ...
  temp.append('hot')
   ...
  temp.append('cool')
于 2013-07-06T23:09:31.130 に答える