how to search for a capital letter within a string and return the list of words with and without capital letters

前端 未结 9 1185
无人共我
无人共我 2020-12-18 23:23

My homework assignment is to Write a program that reads a string from the user and creates a list of words from the input.Create two lists, one containing the words that con

9条回答
  •  萌比男神i
    2020-12-18 23:50

    Hint: "Create two lists"

    s= input("Enter your string: ")
    withcap = []
    without = []
    for word in s.strip().split():
        # your turn
    

    The way you are using the for .. else in is wrong - the else block is executed when there is no break from the loop. The logic you are trying to do looks like this

    for c in s:
        if c.isupper():
            # s contains a capital letter
            # 
            break # one such letter is enough
    else: # we did't `break` out of the loop
        # therefore have no capital letter in s
        # 
    

    which you can also write much shorter with any

    if any(c in "ABCDEFGHIJKLMNOPQRSTUVWXYZ" for c in s):
         # 
    else:
         # 
    

提交回复
热议问题