Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
storing conditions
#1
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.
Reply
#2
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?
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
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
Reply
#4
first of all, don't use str as variable name. it's a built-in function
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Taking user input and storing that to a variable then storing that variable to a list jowalk 12 37,398 Mar-27-2017, 11:45 PM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020