Python case conversion program creation question

Asked 1 weeks ago, Updated 1 weeks ago, 1 views

ss=input('original string =>')
a=0
b=len(ss)
while a<b:
    ss[a].isupper()
    a+=1
if (ss.isupper()) == True:
        print(ss.lower())

else:
        print(ss.upper())

If I type in the letter PYthon, I want to make the result come out like pyTHON

Original string => pyTHon    
>>>PYTHON

Here's the result What should I do?

python

2022-09-20 10:54

1 Answers

Hello.

For Python, indentation is very important. Because it's the grammar of this language in itself. It means that if you tap a few times less, or if you only have a few spaces left, the content of the code may change completely.

In the code of the text, there was no meaning to use the while sentence, and the conditional sentence below is also supposed to change the entire original string, not each letter, so it doesn't seem to work as intended.
Also, since Python's output statement is essentially followed by an opening (\n), you should give an end value to prevent it from being opened.

I wrote it in a relatively desirable way, so please interpret it slowly one by one.

ss = input("Original string=>") # Enter the original string

a = 0
b = len(ss)
while a < b: # Repeat from 0 to ss
    if (ss[a].isupper(): # If the a-th letter of ss is uppercase
        print(ss[a].lower(), end='') # Output in lowercase
    else: # If the a-th letter of ss is not a capital letter
        print(ss[a].upper(), end='') # Output in uppercase
    a + = 1 # Since the repeat is over once, increase the value of a

I thought I could make the lower part more efficiently, so I made it using the for statement as a reference. There's a way like this, so I think it'll be good if you take it as that.

ss = input ("original string =>")

For sins: # Get ss one letter at a time and substitute s for s
    If s.isupper(): #s is uppercase
        print(s.lower(), end="") #s in lowercase
    else: If #s is not a capital letter,
        print(s.upper(), end="") #s in uppercase

~~Maybe~~You can see the code written more intuitively.


2022-09-20 10:54

If you have any answers or tips


© 2022 pinfo. All rights reserved.