I have question that is we will ask user to input number like 2355597 and it will give answer like this 2466618(odd to even) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

I have question that is we will ask user to input number like 2355597 and it will give answer like this 2466618(odd to even)

In this problem we have to convert odd digits to even except 9 to 1 and even remains same.I want answer in python

22nd Jun 2019, 1:26 PM
Shelly Kapoor
Shelly Kapoor - avatar
8 Answers
+ 2
Shelly Kapoor Say the input is "9" inp.index(9) #means position 9 in the list (index) which is 0 since 9 is the first and only element in the list. So now inp.index(9) = 0 inp[inp.index(9)] = 1 #means inp[0] = 1 So this means get the first element in the list and make it 1 "only works is this is 9"
22nd Jun 2019, 3:11 PM
Mo Hani
Mo Hani - avatar
+ 12
Hello !! This place is for asking programming related doubts. No one will solve the questions that you'll post here. If you want help then you have to show your attempt first or tell exactly where you are struct in the problem.
22nd Jun 2019, 2:03 PM
Nova
Nova - avatar
+ 10
This may work : a=input("enter the value") # take normal input list1 = [] # declare a blank list res = [int(x) for x in str(a)] #store individual integer as a list for i in res: # run for loop for each element if i%2!=0: # check if the specific element is odd or not if i==9: #if it is odd and equal to 9 store 1 at that point i=1 else: # if it is odd and equal not to 9 make that number to even i=i+1 list1.append(i) # store each element in a separate list with each iteration print(list1) # print the list
22nd Jun 2019, 2:55 PM
Nova
Nova - avatar
+ 5
print(int(''.join(['1'if c=='9'else str(int(c)+1)if int(c)%2 else c for c in str(2355597)])))
22nd Jun 2019, 6:52 PM
Anna
Anna - avatar
+ 4
@keval a=int(input("enter the value")) x=list(str(a)) for i in range x: if i%2!=0: i=i+1 elif i==9: i=i-1 else : i print(x)
22nd Jun 2019, 2:17 PM
Shelly Kapoor
Shelly Kapoor - avatar
+ 4
thnx both Mo hani and keval
22nd Jun 2019, 3:23 PM
Shelly Kapoor
Shelly Kapoor - avatar
+ 3
@Mo hani can u please explain the inp[inp.index(i)] method
22nd Jun 2019, 2:40 PM
Shelly Kapoor
Shelly Kapoor - avatar
+ 1
Remember to show us your own attempt first next time. This is one way to do it: inp = list(input()) for i in inp: if int(i)%2!=0: if int(i) == 9: inp[inp.index(i)] = 1 else: inp[inp.index(i)] = int(i)+1 This is what the code does: #gets input and makes it list #loops for elements in list #checks if the element is odd #checks for 9, replaces it with 1 #other than 9, adds one to it
22nd Jun 2019, 2:06 PM
Mo Hani
Mo Hani - avatar