简体   繁体   中英

TypeError: 'NoneType' object is not iterable in Phyton

My program removes the substring 'rotten' from the string list:

bag_of_fruits = ["apple","rottenBanana","apple"]
    
def remove_rotten(bag_of_fruits):
    bag_of_fruits =  [x.removeprefix('rotten') for x in bag_of_fruits]
    return [x.lower() for x in bag_of_fruits]

print(remove_rotten(bag_of_fruits))

All tests is completed, but in the end program shows 'Unexpected exception raised':

Traceback (most recent call last):
  File "/workspace/default/.venv/lib/python3.10/site-packages/codewars_test/test_framework.py", line 112, in wrapper
    func()
  File "/workspace/default/tests.py", line 21, in fixed_tests
    test.assert_equals(remove_rotten(tst[0]), tst[1], f"Input = {tst[0]}")
  File "/workspace/default/solution.py", line 4, in remove_rotten
    bag_of_fruits =  [x.removeprefix('rotten') for x in bag_of_fruits]
TypeError: 'NoneType' object is not iterable

Try to change the name of the variable to avoid the error

bag_of_fruits = ["apple","rottenBanana","apple"]

def remove_rotten(bag_of_fruits):
    # ---- > variable name should be changed 
    bag_of_fruits_edited =  [x.removeprefix('rotten') for x in bag_of_fruits]
    return [x.lower() for x in bag_of_fruits_edited]

print(remove_rotten(bag_of_fruits))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM