Answer to Question #254609 in Python for Nics

Question #254609

Cubes and Squares

by CodeChum Admin

You can also make a list of numbers, ey? Let's go and have some fun in tweaking the values of each element, but let's give it some twist to add on some fun to the code.

Let's have a go at it!

Instructions:

  1. An array containing 40 integer elements is already provided for you in the code editor below.
  2. Using loops and conditions, print out the cube of the array element if it is a positive number, and print out the square of the number if it is negative. Each result must be printed out separately by each line.

Instructions

  1. A list containing 5 integer elements is already provided for you in the code editor below.
  2. nums = [-2, -1, 1, 2, 3]
  3. Using loops and conditions, print out the cube of the list element if it is a positive number, and print out the square of the number if it is negative. Each result must be printed out separately by each line.

 

Output

The squares and cubes of the elements in the array.

4

1

1

8

27

.

.

.


1
Expert's answer
2021-10-21T14:22:51-0400
nums = [-2, -1, 1, 2, 3]
for num in nums:
    if num > 0:
        print(num ** 3)
    else:
        print(num ** 2)

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS