我設法解決我的代碼,但我無法弄清楚如何使布爾部分工作。我們應該使用not
運營商,但我不太知道什麼是使用它的正確方法:布爾'不'運算符不能正常工作
def copy_me(list_input):
''' (list) -> list
A function takes as input a list, and returns a copy
of the list with the following changes:
Strings have all their letters converted to upper-case
Integers and floats have their value increased by 1
booleans are negated (False becomes True, True becomes False)
Lists are replaced with the word 」List」
The function should leave the original input list unchanged
>>> copy_me(["aa", 5, ["well", 4], True)
['AA', 6, 'List', False]
>>> copy_me([20932498, 4], 5.98, "And", False)
['List', 6.98, 'AND', True]
'''
# if element is a string, change all the letters to upper case
# if element is an integer or float, have their value increased by 1
# if element is a boolean, negate it
# if element is a list, replace it with the word "List"
new_list = list_input[:]
for index in range(len(new_list)):
if isinstance(new_list[index], str):
new_list[index].upper()
elif isinstance(new_list[index], int):
new_list[index] += 1
elif isinstance(new_list[index], float):
new_list[index] += 1.0
elif isinstance(new_list[index], list):
new_list[index] = "List"
elif isinstance(new_list[index], bool):
not new_list[index]
return new_list
哪來的分配? – 2014-10-29 20:28:23