Aug-24-2020, 09:03 AM
I used following code for printing each word of a string in reveres order.
str=input("Enter a string : ") i=0 j=0 k=0 # stored condition 1 :- strIsAlpha=str[j]>='a' and str[j]<='z' or str[j]>='A' and str[j]<='Z' # stored condition 2 :- strIsNotAlpha=not(str[i]>='a' and str[i]<='z') and not(str[i]>='A' and str[i]<='Z') while i<len(str): j=i while str[j]>='a' and str[j]<='z' or str[j]>='A' and str[j]<='Z': j+=1 if j==len(str): break k=j j-=1 while j>=i: print(str[j],end='') j-=1; print() i=k if i<len(str): while not(str[i]>='a' and str[i]<='z') and not(str[i]>='A' and str[i]<='Z'): i+=1 if i==len(str): break print("Loop over.")Got the anticipated output :-
Output:Enter a string : this is a string
siht
si
a
gnirts
Loop over.
But when I used stored conditions i.e., "strIsAlpha" and "strIsNotAlpha", as shown below : - str=input("Enter a string : ") i=0 j=0 k=0 # stored condition 1 :- strIsAlpha=str[j]>='a' and str[j]<='z' or str[j]>='A' and str[j]<='Z' # stored condition 2 :- strIsNotAlpha=not(str[i]>='a' and str[i]<='z') and not(str[i]>='A' and str[i]<='Z') while i<len(str): j=i while strIsAlpha: j+=1 if j==len(str): break k=j j-=1 while j>=i: print(str[j],end='') j-=1; print() i=k if i<len(str): while strIsNotAlpha: i+=1 if i==len(str): break print("Loop over.")I got a changed output as show below :-
Output:Enter a string : this is a string
Loop over.
I am unable to understand the change in output despite the conditions being same. only stored this time. Could anyone please explain this.