# Get 5 to 10 characters and flip their case

This small programme is an exercise that my teacher gave us.

n is an alphabetic input that its length must be between 5 and 10. You have to insert the characters of the input one by one to a list called T. Then, add the reverse case of letters to another list called V. So if there is a capital letter in T it would be lower in list V and you have to only print V at the end.

This is my code, is there any better solution or cleaner one perhaps?

T = []
V = []
n = input("Enter a character")
while len(n) not in range(5, 11) or n.isalpha() == False:
n = input("Enter a characters between 5 and 11, and only characters ")

print(n)
for i in range(len(n)):
T.append(n[i])

for i in range(len(T)):
if T[i].isupper():
V.append(T[i].lower())
else:
V.append(T[i].upper())

print(V)

• Welcome to Code Review! The current question title is too general to be useful here. Please edit to the site standard, which is for the title to simply state the task accomplished by the code. Please see How to get the best value out of Code Review: Asking Questions for guidance on writing good question titles. Apr 25, 2021 at 21:54
• Your "N is a alphabetic input that is between 5 and 10" does not really make sense. Are you sure you copied your task correctly? Apr 25, 2021 at 22:20
• N is basically a string input that have 5 characters atleast and doesn't go beyond 10 characters, it should not be numbers or symbols. Apr 25, 2021 at 23:02
• There is no N in your code snippet, and it is unclear what "between 5 and 10" means. Apr 25, 2021 at 23:02
• i'm sorry if i couldn't explain more clearly, im not a native english speaker. Apr 25, 2021 at 23:04

A flexible approach to getting user input is to use a while-true loop, breaking when conditions are met. The structure is easy to remember and allows you to handle user mistakes without awkward setup or code repetition:

while True:
x = input('...')
if x is OK:
break


I realize the variable names are coming from your teacher's instructions, but both of you should strive for better variable names. Better does not always mean longer (brevity and clarity are both worthy goals and they are at odds sometimes); but it does mean using variable names that are meaningful in context. For example, letters is a better name for a string of letters than n, which is a purely abstract name (even worse, it adds confusion because n is often/conventionally used for numeric values).

There is no need to build T character by character. Python strings are iterable and therefore directly convertible to lists.

Python strings, lists, and tuples are directly iterable: just iterate over the values and don't bother with the soul-crushing tedium imposed by many less-cool programming languages where you must iterate over indexes. And for cases when you need both values and indexes, use enumerate().

Use simple comments as sign-posts and organizational devices to make your code more readable. This habit will serve you well as you try to write bigger, more complex programs.

# Get user input.
while True:
prompt = 'Enter 5 to 10 letters, without spaces or punctuation: '
letters = input(prompt)
if len(letters) in range(5, 11) and letters.isalpha():
break

# List of the original letters.
T = list(letters)

# List of letters with case flipped.
V = []
for c in letters:
c2 = c.lower() if c.isupper() else c.upper()
V.append(c2)

# Same thing, in one shot.
V = [
c.lower() if c.isupper() else c.upper()
for c in letters
]

# Check.
print(T)
print(V)

• Writing comments really came in handy, I am working on this keylogger, i end up giving up because its a 110 line and its literally a mess. I truly appreciate your help, you deserve to be a teacher. Apr 25, 2021 at 23:00

First I suggest you use more functions. That makes your code more organised and allows you to reuse code later. It also provides an overview to what each piece of code does. I would create a function for getting the user input, one for swapping cases and one for printing the output.

Separate the user input from what your code does. Also, separate this from the output. This will create a nice structure of your code.

Don't reinvent the wheel. Python has a built-in function for swapping cases which is aptly named:string.swapcase(). If you are not allowed by your teacher to use the built-in function, then forget this point.

Wrap your code in a main() method. This will allow you to import your module somewhere else and use the functions you have defined. Then add the following to run main:

if __name__ == '__main__':
main()


Putting this all together:

def ask_for_string() -> str:
"""ask the user for a string containing 5 to 10 characters"""
while True:
my_str = input("Enter a string of length 5 to 10, consisting of only characters: ")
if my_str.isalpha() and 4 < len(my_str) < 11:
return my_str

def print_output(T:list, V: list):
"""print user input and list of swapcased characters """
print("List of characters you entered:", T)
print("your list of swapped characters is:", V)

def main():