[英]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. 您不能使用的功能
upper
和lower
的整数。 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.