简体   繁体   English

如何使用范围将列表中的单词更改为大写/小写-Python

[英]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". 删除了未使用的“ list_len”变量,在“ words_list”中循环并检查“ word”的长度。 And concentrate output with "out" variable. 并使用“ out”变量集中输出。 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. 使用适当的IDE进行编码,例如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. 如果您不知道发生了什么,请在需要的地方放一些print 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 您也可以使用新列表来保存输出,而不是替换原始列表中的值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. word是迭代器的值,因此是整数。 You can't use the functions upper and lower on integer. 您不能使用的功能upperlower的整数。 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 ): 您还可以使其函数返回列表(也使用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']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python:如何在句子的单词列表中找到一个字母并以原始大小写返回这些单词(大写/小写) - Python: How to find a letter in a sentence's list of words and return those words in their original case (upper/lower) 如何在Python中组织大小写单词? - How do I organize lower and upper case words in Python? Python中改变大写和小写的简单代码 - Simple Code in Python to change the Upper Case and Lower Case 我如何在python3中将大小写从大写翻转为小写以及从小写翻转为大写 - How can i flip Case from upper to lower case and from lower to upper case in python3 如何将大写字母更改为小写字母并将空格更改为下划线 - How to change upper case letters to lower case letters and spaces to underscores 如何使用循环创建 function 以返回 python 上大写和小写字母的数量? - How do I create a function using loops to return the number of upper case and lower case letters on python? 解释Python中的大小写 - Account for upper and lower case in Python 用大写和小写分割多个连接的单词 - Split multiple joined words with upper and lower case Python-从字符串列表中删除小写或大写字母? - Python - Removing lower or upper case letters from a list of strings? 如何通过for循环根据Python中的范围创建下限值和上限值列表? - How to create a list of lower and upper bound values based on range in Python via for loop?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM