Guys i dont understand it 🤔 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 6

Guys i dont understand it 🤔

Guys im studying while loops in python,and with this knowledge i tried do a while loop (i took a example of the python course) and i dont understand why get out this output Link : https://code.sololearn.com/c0SHIcjW08qi/?ref=app

2nd May 2021, 3:02 PM
Katdotbrush
Katdotbrush - avatar
11 Answers
+ 7
x = 1 while x < 10: if x == 0: print (str(x) + "Hi man") else: print (str(x) + "Hi woman") First this: if x == 0: print (str(x) + "Hi man") else: print (str(x) + "Hi woman") x is not equal to 1, so the if statement become False, It will evaluate else statement, which is: else: print (str(x) + "Hi women") This will print 1Hi women Now, the while statement, which is while x < 10: 1(value of x) is smaller than 10, so the statement will keep repeating until x becomes greater than 10. And you made infinite loop, because x will never become greater than 10. Isn't it's like English??
2nd May 2021, 3:26 PM
Scarlet Witch
Scarlet Witch - avatar
+ 4
Hey cat cute Value of x is 1 , while's condition evaluates to true , then else will evaluate but when else condition ends it goes again in while loop , it again satisfies condition it , again else condition will be evaluated this happen so on goes to infinity (system may be crash) ! 1. Put a condition were while's condition will false then and only it will terminates !
2nd May 2021, 3:13 PM
Giriraj Yalpalwar
Giriraj Yalpalwar - avatar
+ 4
In your program you would not use increment operator that's why the value of x is not incremented so your code will be executed infinite times. x=1 while x<10: if x==0: print (str(x) +"Hi man") else: print (srt(x) +"Hi woman") x+=1
3rd May 2021, 4:12 AM
srishti gupta
srishti gupta - avatar
+ 3
cat cute, no problem👍🏻
2nd May 2021, 3:54 PM
Scarlet Witch
Scarlet Witch - avatar
+ 2
cat cute According to your code the process will run till infinite times since there is no increment of x So every time x will be 1. If you want to get desire output then just add x += 1 after else part x = 1 while x < 10: if x == 0: print (str(x) + "Hi man") else: print (str(x) + "Hi woman") x += 1
2nd May 2021, 4:16 PM
A͢J
A͢J - avatar
+ 2
You just forget to increment the loop var, thats a common error ;)
3rd May 2021, 9:32 PM
6hpxq9
6hpxq9 - avatar
+ 1
Scarlet Witch thank you man,you helped me a lot
2nd May 2021, 3:52 PM
Katdotbrush
Katdotbrush - avatar
+ 1
There is no termination condition
4th May 2021, 9:55 AM
Rishi
Rishi - avatar
+ 1
Am new plz guide me
4th May 2021, 11:33 AM
sM star
sM star - avatar
- 1
sM star calm down,you are just in the begin of your course,continue studying python :) And dont give up,try understand the code Hugs 🇧🇷👍
4th May 2021, 11:58 AM
Katdotbrush
Katdotbrush - avatar
- 2
Look back at the lesson.
2nd May 2021, 3:24 PM
zxtychj
zxtychj - avatar