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
My regex:
vendor = "MyNameIsJoe. IWorkInDDFinc."
ven = re.split(r'(?<=[a-z])[A-Z]|[A-Z](?=[a-z])', vendor)
I need split word that would have happened:
My Name Is Joe. I Work In DDF inc.
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
# <do something>
break # one such letter is enough
else: # we did't `break` out of the loop
# therefore have no capital letter in s
# <do something>
which you can also write much shorter with any
if any(c in "ABCDEFGHIJKLMNOPQRSTUVWXYZ" for c in s):
# <do something>
else:
# <do something>
Thank you to everyone for your input and help, it was all very informative. Here is the answer that I finally submitted.
s = input("Please enter a sentence: ")
withcap = []
without = []
for word in s.split():
if word.islower():
without.append(word)
else:
withcap.append(word)
onelist = withcap + without
for word in onelist:
print (word)