当前位置:   article > 正文

Learn the basics of Python 3-Code Challenges:Loops

Learn the basics of Python 3-Code Challenges:Loops

   1.Coding question 1 Divisible By Ten

Create a function named divisible_by_ten() that takes a list of numbers named nums as a parameter.
Return the count of how many numbers in the list are divisible by 10.

  1. def divisible_by_ten(nums):
  2. count = 0
  3. for number in nums:
  4. if (number % 10 == 0):
  5. count += 1
  6. return count
  7. print(divisible_by_ten([20, 25, 30, 35, 40]))

 2.Coding question 2 Greetings

Create a function named add_greetings() which takes a list of strings named names as a parameter.In the function, create an empty list that will contain each greeting. Add the string 'Hello, ' in front of each name in names and append the greeting to the list.Return the new list containing the greetings.

  1. def add_greetings(names):
  2. new_list = []
  3. for name in names:
  4. new_list.append("Hello, " + name)
  5. return new_list
  6. print(add_greetings(["Owen", "Max", "Sophie"]))

 3.Coding question 3

Write a function called delete_starting_evens() that has a parameter named my_list.
The function should remove elements from the front of my_list until the front of the list is not even. The function should then return my_list.
For example if my_list started as [4, 8, 10, 11, 12, 15], then delete_starting_evens(my_list) should return [11, 12, 15].
Make sure your function works even if every element in the list is even!

  1. #Write your function here (Can you find the problem?)
  2. def delete_starting_evens(my_list):
  3. for num in my_list:
  4. if num % 2 == 0:
  5. my_list.remove(num)
  6. else:
  7. break
  8. return my_list
  9. print(delete_starting_evens([4, 8, 10, 11, 12, 15]))
  10. # =>[8, 11, 15]
  11. print(delete_starting_evens([4, 8, 10]))
  12. # =>[8]
  13. #This is the way we solved it:
  14. def delete_starting_evens(my_list):
  15. while (len(my_list) > 0 and my_list[0] % 2 == 0):
  16. my_list = my_list[1:]
  17. return my_list
  18. print(delete_starting_evens([4, 8, 10, 11, 12, 15]))
  19. # =>[11, 12,15]
  20. print(delete_starting_evens([4, 8, 10]))
  21. # =>[]

4.Coding question 4 Odd Indices

Create a function named odd_indices() that has one parameter named my_list.
The function should create a new empty list and add every element from my_list that has an odd index. The function should then return this new list.
For example, odd_indices([4, 3, 7, 10, 11, -2]) should return the list [3, 10, -2].

  1. def odd_indices(my_list):
  2. new_my_list = []
  3. for i in range(len(my_list)):
  4. if i % 2 == 1:
  5. new_my_list.append(my_list[i])
  6. return new_my_list
  7. print(odd_indices([4, 3, 7, 10, 11, -2]))
  8. # =>[3, 10, -2]
  9. #Here is this solution:
  10. def odd_indices(my_list):
  11. new_list = []
  12. for index in range(1, len(my_list), 2):
  13. new_list.append(my_list[index])
  14. return new_list
  15. print(odd_indices([4, 3, 7, 10, 11, -2]))
  16. # =>[3, 10, -2]

 5.Coding question 5 Exponents

Create a function named exponents() that takes two lists as parameters named bases and powers. Return a new list containing every number in bases raised to every number in powers.
For example, consider the following code.exponents([2, 3, 4], [1, 2, 3])

  1. def exponents(bases, powers):
  2. my_list = []
  3. for num1 in bases:
  4. for num2 in powers:
  5. my_list.append(num1**num2)
  6. return my_list
  7. print(exponents([2, 3, 4], [1, 2, 3]))
  8. # =>[2, 4, 8, 3, 9, 27, 4, 16, 64]
  9. #Here is how we solved this one:
  10. def exponents(bases, powers):
  11. new_list = []
  12. for base in bases:
  13. for power in powers:
  14. new_list.append(base ** power)
  15. return new_list
  16. print(exponents([2, 3, 4], [1, 2, 3]))
  17. # =>[2, 4, 8, 3, 9, 27, 4, 16, 64]

Learn the basics of Python 3-Chapter 4:Loops

Learn the basics of Python 3-Code Challenges:Loops(Advanced)

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/43113
推荐阅读
相关标签
  

闽ICP备14008679号