Nested functions | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
0

Nested functions

How to write a program that has two functions, called prime() and PrimeOrNot(). The function prime() will take objects in a list L, and it will do the following: 1) It (prime()) will send each element of the list L, one at a time, to the function PrimeOrNot() 2) When an answer is returned from PrimeOrNot(), prime() will add that to a list P(P is initially empty). The function PrimeOrNot() will take in each element of L, from prime(), one at a time, and determine whether it is prime or not. If it is, it will return the value ā€˜Yesā€™ back to prime(). Otherwise, it will return the value ā€˜Noā€™ back to prime(). My attempt: L=[1, 2,3,4] P=[] def Prime()?? def PrimeOrNot() : if num>1: for i in L: if (num%I)==0: print(i, 'is not prime') else: print(I, 'is prime') Q: How do I get prime() to send elements of L to PrimeOrNot() 1 at a time?

3rd Jun 2020, 2:04 AM
Nigel George
Nigel George - avatar
1 Resposta
0
First define your prime function that takes the list as an argument Define the prime_or_not(num) inside it, you want it to take one number at a time so you will pass only one argument that is a number from the list Then try a for loop inside the prime that iterates over the list and pass each number to the prime_or_not which returns an answer That is perhaps one way to do it, you can come up with a better solution. Good luck
3rd Jun 2020, 2:57 AM
Ali Abdelhady
Ali Abdelhady - avatar