简体   繁体   中英

How to change words in a list to upper/lower case using range - Python

One part of my assignment is to get user input, convert that into a list, then depending on the number of characters in each word, change to upper/lower case accordingly. I have been told I have to use range, which is the part I am struggling with. This is my latest attempt but it's not working. Any advice would be appreciated.

poem = input("Enter a poem, verse or saying: ")
words_list = poem.split()
list_len = len(words_list)
for word in range(0,list_len):
    if len(words_list[word]) < 4:
        word = word.lower()
    elif len(words_list[word]) > 6:
        word = words.upper()

How about this?

poem = input("Enter a poem, verse or saying: ")
words_list = poem.split()
out = ""
for word in words_list:
    if len(word) < 4:
        word = word.lower()
    elif len(word) > 6:
        word = word.upper()
    out += " " + word
print(out)

Removed uneeded "list_len" variable, looped in "words_list" and checking length of "word". And concentrate output with "out" variable. For output there could be better techniques, I just did an easy one.

poem = input("Enter a poem, verse or saying: ")
words_list = poem.split()
list_len = len(words_list)

# word is not a word here, it was an integer
# by a convention we use 'i' for the iterator here
for i in range(0,list_len):
    # here we assign an actual 'word' to the word variable
    word = words_list[i]
    if len(word) < 4:
        # therefore you couldn't use .lower() or .upper() on the integer
        word = word.lower()
    elif len(word) > 6:
        word = word.upper()

Use a proper IDE for coding like PyCharm. It would alert you about all the mistakes you've made.

That will work if you really need to use range, but you still need to figure out printing / returning the value.

If you don't know what's going on just put some print s wherever you need. You would figure it out by yourself if you put prints in your code.

Just a small modification to your original code. Since you want to convert to upper/lower case, you also want to probably save the output. You can alternatively use a new list to save your output rather than replacing the values in the original list words_list

for word in range(0,list_len):
    if len(words_list[word]) < 4:
        words_list[word] = words_list[word].lower()
    elif len(words_list[word]) > 6:
        words_list[word] = words_list[word].upper()

print(words_list)

Output

Enter a poem, verse or saying: My name is bazingaa
['my', 'name', 'is', 'BAZINGAA']

Your code has a several problems. The word is the value of the iterator and so it is an integer. You can't use the functions upper and lower on integer. However, you can solve it in an easier way:

poem = input("Enter a poem, verse or saying: ")
words_list = poem.split()
for i in range(0,len(words_list)):
    word = words_list[i]
    word = word.lower() if len(word) < 4 else word
    if len(word) > 6:word = word.upper()
    print(word)

On executing the above code:

Enter a poem, verse or saying: Programming in Python is great
PROGRAMMING
in
Python
is
great

You can also make its function which returns a list (also uses range ):

def get(string):
    words_list = string.split()
    output = []
    for i in range(0,len(words_list)):
        word = words_list[i]
        word = word.lower() if len(word) < 4 else word
        if len(word) > 6:word = word.upper()
        output.append(word)
    return output

print(get(input("Enter a poem, verse or saying: ")))

Testing:

Enter a poem, verse or saying: Programming in Python is great
['PROGRAMMING', 'in', 'Python', 'is', 'great']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM