I don't understand this code. Can someone explain it please? | Sololearn: Learn to code for FREE!


I don't understand this code. Can someone explain it please?

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

6/20/2021 1:26:52 PM

Mandana Hazarian

7 Answers

New Answer


Simply your loop will run total 9 times it will start from 1 to till 10 . Write all numbers between 1 to 10 on copy and divide it with 2 . Here ( % ) symbol represent remaider it is also known as modules . If reminder is zero then first if condition will execute and it will print that number but in print u used str () so number will convert into string and is even will also print. But if you will get remaider any values rather than zero then it Will print odd 2%2==0 even 3%2!=0 odd 4%2==0 even 5%2!=0 odd ...... ...... and your x+=1 it will increase loop values one by one do dry run on copy you will understood much better


Simple code which have : a while loop runs from x=1 to x<10 becomes false(i.e till 10<10), in which If part executes and prints , when x is even Else part executed and prints , when x is odd. Every time x is incremented by 1 in each iteration by x+=1 mention where you not understanding, to understand your problem clearly.....


You have assign as variable name x with value 1 Now you are running a loop, your loop will run until the condition became false, So value of x is 1 ,which satisfy the condition x < 10 , so the whole thing inside this block will execute, now it will check x % 2 == 0 or not, In mathematics a number is divisible by a number, if it's remender is 0 ,by dividing that number, so we are saying that the x is divisible by 2 or not, if the condition is true the it will print "x is odd number" , we know that x is 1 so it will print "1 is odd number" Then in last line x will become 2 , and then the above process will go on


basically it tells you if the number is odd or even for every number from 1 to 9 inclusive


#While loop stop run when the statement in while loop is False x = 1#1,x==1..2,X==2 while x < 10:#(1 the first loop),1is less than 10 ==True..(2second loop),2is less than 10 ==True if x%2 == 0:#(x==1)%2!=0==False &the statement is not run 2, but now X is 2 is 2 is even number and the statement is True and if body is run print(str(x) + " is even") else:# if if statement is not True else statement is run print(str(x) + " is odd")#and this is print x += 1#this is used to count a Number when while loop is iterate The loop is stop when X==10 or While ( X==10)<10: because 10 is not less than 10


Alternate form, which may be easier to understand: for x in range(10): print(x, 'is', 'odd' if x%2 else 'even')


Am looking for someone who can be devoted to teach me this because I seriously need it please if you're interested message me please