Python—循环中字符串中每隔一个字母大写,其中数据是从复制的用户输入列表传递的

Python—循环中字符串中每隔一个字母大写,其中数据是从复制的用户输入列表传递的,python,list,Python,List,我被要求让用户在列表中输入至少8个单词,然后对列表中的数据执行各种操作。它要求我做的一个操作是创建一个循环,使字符串中的每一个字母都大写(hElLo WoRlD)。为了更好的可读性,我省略了对代码所做的其他操作 import sys def main(): words = [] wordCount = 0 userWord = input("Enter at least 8 words or 'bye' to leave the program: ").split(' '

我被要求让用户在列表中输入至少8个单词,然后对列表中的数据执行各种操作。它要求我做的一个操作是创建一个循环,使字符串中的每一个字母都大写(hElLo WoRlD)。为了更好的可读性,我省略了对代码所做的其他操作

import sys
def main():
    words = []
    wordCount = 0
    userWord = input("Enter at least 8 words or 'bye' to leave the program: ").split(' ')
    while True:
        if len(userWord)<8:
            print("Please print at least 8 words, try again.")
            sys.exit()
        elif wordCount >= 8 and userWord[wordCount] != 'bye':
            words.append(userWord[wordCount])
            wordCount = wordCount + 1
        else:
            break
     every_other (userWord)
def every_other(words):
    words6 = words.copy()
    st = ""
    for i in range(len(words6)):
        if (i%2) == 0:
            st += words6[i].upper()
        else:
            st += words6[i]
     print ('This is your list with every other letter capitalized: ', words6)
     return st
main()
导入系统 def main(): 单词=[] 字数=0 userWord=input(“输入至少8个单词或‘再见’以退出程序:”)。拆分(“”) 尽管如此: 如果len(userWord)=8和userWord[wordCount]!='再见: 追加(userWord[wordCount]) 字数=字数+1 其他: 打破 每隔一个(用户名) def每_其他(文字): words6=words.copy() st=“” 对于范围内的i(len(words6)): 如果(i%2)==0: st+=words6[i].上() 其他: st+=words6[i] 打印('这是您的列表,每隔一个字母大写:',单词6) 返回街 main()
我没有收到任何错误消息,但代码似乎不是每隔一次从def开始运行。

您必须每隔一次打印函数
,因为它返回一个字符串:

import sys
def main():
    words = []
    wordCount = 0
    userWord = input("Enter at least 8 words or 'bye' to leave the program: ").split(' ')
    while True:
        if len(userWord)<8:
            print("Please print at least 8 words, try again.")
            sys.exit()
        elif wordCount >= 8 and userWord[wordCount] != 'bye':
            words.append(userWord[wordCount])
            wordCount = wordCount + 1
        else:
            break
    print('This is your list with every other letter capitalized: ', every_other(userWord))
def every_other(words):
    words6 = words.copy()
    st = ""
    for i in range(len(words6)):
        if (i%2) == 0:
            st += words6[i].upper()
        else:
            st += words6[i]
    return st
    #print ('This is your list with every other letter capitalized: ', words6) # This will never run as the function has already returned
main()
导入系统 def main(): 单词=[] 字数=0 userWord=input(“输入至少8个单词或‘再见’以退出程序:”)。拆分(“”) 尽管如此: 如果len(userWord)=8和userWord[wordCount]!='再见: 追加(userWord[wordCount]) 字数=字数+1 其他: 打破 print('这是您的列表,每隔一个字母大写:',每隔一个字母(userWord)) def每_其他(文字): words6=words.copy() st=“” 对于范围内的i(len(words6)): 如果(i%2)==0: st+=words6[i].上() 其他: st+=words6[i] 返回街 #print('这是您的列表,每隔一个字母大写:',words6)#由于函数已返回,因此此操作永远不会运行 main()
如果要将每秒钟的字符大写,请执行以下操作:

import sys
def main():
    words = []
    wordCount = 0
    userWord = input("Enter at least 8 words or 'bye' to leave the program: ").split(' ')
    while True:
        if len(userWord)<8:
            print("Please print at least 8 words, try again.")
            sys.exit()
        elif wordCount >= 8 and userWord[wordCount] != 'bye':
            words.append(userWord[wordCount])
            wordCount = wordCount + 1
        else:
            break

    print('This is your list with every other letter capitalized: ', every_other(userWord))


def every_other(words):
    st = ""
    new_st = ""
    for w in words:
        st+=w
    print(str(st))
    for count, val in enumerate(st):
        if (count % 2) == 0:
            val = val.upper()

        new_st+=val
    return new_st

main()
导入系统 def main(): 单词=[] 字数=0 userWord=input(“输入至少8个单词或‘再见’以退出程序:”)。拆分(“”) 尽管如此: 如果len(userWord)=8和userWord[wordCount]!='再见: 追加(userWord[wordCount]) 字数=字数+1 其他: 打破 print('这是您的列表,每隔一个字母大写:',每隔一个字母(userWord)) def每_其他(文字): st=“” new_st=“” 对于w,大写: st+=w 印刷品(str(st)) 对于计数,枚举中的val(st): 如果(计数%2)==0: val=val.upper() new_st+=val 返回新地址 main()
它正在中断,因为如果您输入8个单词,在while循环的第一次迭代中,wordcount仍然是0,因此它将命中最后一个“else”,这将中断整个循环。
words6.copy
?那正是我们昨天吃的,对吗?那么,这只是你家庭作业中的下一个任务吗?函数调用昨天起作用了-今天,我看到了空格…
print('这是你的列表,每个字母都大写:',words6)
位于
return st
@nostradamus之后我保证我不会用Stack做我所有的作业。我对Python非常陌生,对一些我无法理解的事情很感兴趣,这就是为什么我在这里发帖的原因。我总共要对数据进行15次操作。确切地说,只是为了澄清,@SSparks在返回st后试图打印,然后就不会打印了。words6是一个列表,所以我们不是在重复字符,而是在重复单词,所以其他单词都是大写的。我对Python非常陌生。在Python中,我最难理解的是事物的位置以及程序如何逻辑地读取事物。我不知道为什么它会给我带来这么多问题,但它确实是。谢谢你们两位的帮助@Shadesfear,现在我可以看到它打印出来了,我知道你在说什么了。这就是它对任何列表的工作方式,还是仅仅因为我对它进行了设置,它不会迭代单个字符?当您迭代列表时,它将始终迭代列表中的每个项目。这意味着,如果每个元素都是字符串(或者是单词,如果你愿意的话),那么你的程序总是一字不差地接受它。但是您可以选择,您可以合并列表中的每个元素,因此它只是一个长字符串,read.join。或者,您可以嵌套for循环并迭代每个单词中的每个元素。我对我的回答做了一个小补充,希望能对你有进一步的帮助。请参阅“我的编辑”以了解每秒钟大写一个字符的解决方案。