0

I'm trying to write an array of 5 elements that are user inputted. If the element is divisible by 5, 10 should be added to the element. I have the basic code for the array:

p= [ 0 for i in range(5) ]
print ("Enter an integer number: ")
for i in range (5):
p[i]= int(input())
print ("The modified array is", p)

But I don't know how to modify (i)?

As far as I understand I have to use enumerate, but how s that applied to a input value?

for i,x in enumerate(p):
if x % 5 ==0 :
   p[i] + 5 

But this does not modify the array at all? What am I doing wrong?

1
  • You said "if the element is divisible by 5, 10 should be added" but you are adding 5. Commented Aug 19, 2018 at 13:41

1 Answer 1

1

Store the change made back into p[i]

for i,x in enumerate(p):
    if x % 5 ==0 :
        p[i] = p[i] + 5 

You can change it while asking for input itself :

p=[]
for i in range(5):
    num=int(input())
    if(num%5==0):
        p.append(num+10)
    else:
        p.append(num)

# input : 1 2 3 4 5
# p : 1 2 3 4 15
Sign up to request clarification or add additional context in comments.

4 Comments

This is what I am looking for, thanks, but output is: The modified array is [0, 0, 0, 0, 0, 1, 2, 3, 4, 15] Why is range 10 elements instead of 5?
@WheatBall output is?
"The modified array is [0, 0, 0, 0, 0, 1, 2, 3, 4, 15]"
Initialize p=[] not p= [ 0 for i in range(5) ]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.