178k views
4 votes
Create a list of strings based on a list of numbers The rules:_______

A. If the number is a multiple of five and odd, the string should be 'five odd'
B. If the number is a multiple of five and even, the string should be 'five even'
C. If the number is odd, the string is 'odd'
D. If the number is even, the string is 'even'

User Mmmaaak
by
4.5k points

1 Answer

7 votes

Answer:

listNumbers = [34,56,23,56,78,89,98,45,34,33,25,26,67,78]

listString = [ ]

for i in range(14):

if listNumbers[i]%2!=0 and listNumbers[i]%5==0:

listString.append("five odd")

elif listNumbers[i]%5==0 and listNumbers[i]%2==0:

listString.append("five even")

elif listNumbers[i]%2==0:

listString.append("even")

elif listNumbers[i]%2!=0:

listString.append("odd")

print(listNumbers)

print(listString)

Step-by-step explanation:

In python programming language;

  1. Create two lists
  2. The first is a list of numbers and initialize it with random values: listNumbers = [34,56,23,56,78,89,98,45,34,33,25,26,67,78]
  3. The second list is empty and will hold the string values listString = [ ]
  4. Use a for loop to iterate over all the elementts in the list of numbers
  5. Use the modulo operator (%) to chech for multiples of particular numbers as stipulated by the question
  6. Use combination of if/elif statements for each condition
  7. Use the .append method to add the elements into the list of strings
  8. finially output both lists

See attached code and output

Create a list of strings based on a list of numbers The rules:_______ A. If the number-example-1
User Guru Cse
by
5.2k points