+ 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
11 Réponses
+ 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??
+ 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 !
+ 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
+ 3
cat cute, no problem👍🏻
+ 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
+ 2
You just forget to increment the loop var, thats a common error ;)
+ 1
Scarlet Witch thank you man,you helped me a lot
+ 1
There is no termination condition
+ 1
Am new plz guide me
- 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 🇧🇷👍
- 2
Look back at the lesson.