Python Forum
storing conditions - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: storing conditions (/thread-29234.html)



storing conditions - sureshSinghBisht - Aug-24-2020

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.


RE: storing conditions - perfringo - Aug-24-2020

Is there some particular reason why this implemented in such a complicated way?

Also: relying on ascii characters only makes this code very fragile and how should O’Reilly look like in reversed form?


RE: storing conditions - deanhystad - Aug-24-2020

What you think is a comparison is just a variable that stores the result of the first comparison. In other words:
strIsAlpha=str[j]>='a' and str[j]<='z' or str[j]>='A' and str[j]<='Z'
is not re-evaluated each time through the loop, but rather is frozen at the value of
strIsAlpha=str[0]>='a' and str[0]<='z' or str[0]>='A' and str[0]<='Z'
You could rewrite your comparison to be callable like this:
def strIsAlpha(c):
    return c>='a' and c<='z' or c>='A' and c<='Z'
And you would use it like this
while strIsAlpha(str[j])
One other thing. Do not use Python key words as names in your program. Variable names should represent what they contain, not what type they are. Instead of str use phrase or user_input or input_str or something that says what is contained in the str


RE: storing conditions - buran - Aug-24-2020

first of all, don't use str as variable name. it's a built-in function


RE: storing conditions - deanhystad - Aug-24-2020

(Aug-24-2020, 12:58 PM)buran Wrote: first of all, don't use str as variable name. it's a built-in function
Which means your program can no longer use the function str() because Python will first find the name "str" as a variable in the local name space and will not look for "str" in built-ins. Other commonly and stupidly used variable names are "list", "set", and "dict". It doesn't help that the internet is littered with tutorials that ignore buran's sage advice.