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?
-
You said "if the element is divisible by 5, 10 should be added" but you are adding 5.Sheldore– Sheldore2018年08月19日 13:41:31 +00:00Commented Aug 19, 2018 at 13:41
1 Answer 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
answered Aug 19, 2018 at 13:40
Sruthi
3,0281 gold badge14 silver badges26 bronze badges
Sign up to request clarification or add additional context in comments.
4 Comments
WheatBall
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?
Sruthi
@WheatBall output is?
WheatBall
"The modified array is [0, 0, 0, 0, 0, 1, 2, 3, 4, 15]"
Sruthi
Initialize
p=[] not p= [ 0 for i in range(5) ]lang-py