0

Please give me Explaination for below Program:

x = 1 while x < 10: x += 1 if x%2 == 0: print(str(x) + " is even") else: print(str(x) + " is odd")

22nd Jan 2023, 3:11 PM
Iynesh Karthick
Iynesh Karthick - avatar
3 Answers
+ 7
Iynesh Karthick , 2 small amendments that improves the code a bit: > the number of 1 will not be processed and not *output* since the increment is done just *before* the calculation. do this after the if... else... construct. if you wanted to include the number of 10, use: while x <= 10: > the output of the numbers does not need to be converted to string, if we use the comma notation instead of concatenation with *+* operator. also no extra space is needed in the strings. x = 1 while x < 10: #x += 1 if x%2 == 0: print(x, "is even") else: print(x, "is odd") x += 1
22nd Jan 2023, 5:18 PM
Lothar
Lothar - avatar
+ 3
Iynesh Karthick it's a simple odd or even while loop .. not sure why you are changing a number to a str when you can simply use print(f"{x} is even") and the same with your odd print
22nd Jan 2023, 3:38 PM
BroFar
BroFar - avatar
0
# create variable x and assign it to 1 x = 1 # this is while loop , its iterate the cods inside of it x Less than 10 while x < 10: # increase value of x by 1 x += 1 # if x can be divide by 2 with 0 remainder i.e x is even number then print x is even if x%2 == 0: print(str(x) + " is even") else: print(str(x) + " is odd") #else print x is odd
22nd Jan 2023, 5:33 PM
Thilina Dajahetti
Thilina Dajahetti - avatar