How this works
- There’ll be 4 Python functions, and each does something
- Each function has ONE wrong line
- You need to take steps to identify the ONE wrong line
- Try not to read the comments for clues
If you can’t find the mistakes in these 4 functions, this doesn’t mean that you’re not decent at Python.
It just means you’re not decent at Python YET.
We all start from zero at some point, and you just need enough practice and consistency to go from being not decent to decent!
1) Counting Fruits
Given a list of fruits, the function counts the number of times each fruit appears, and returns a dictionary containing each fruit and its count.
def count_fruits(fruits):
'''
input: ['apple', 'orange', 'apple']
output: {'apple':2, 'orange':1}
'''
out = {}
for fruit in fruits:
if fruit not in out:
out[fruit] = 1
else:
out[fruit] += 1
return out
2) Checking for prime numbers
Here’s a function that takes in an integer, and returns True if the input integer is a prime number, and False otherwise. A prime number is defined as an integer larger than 1 that can only be divided by 1 and itself.