How do I make this program accepts only a user input that's been typed while following a proper capitalization. Like it won't accept "robin hood" unless it's "Robin Hood". When I run it, it says...
Traceback (most recent call last):
File "C:\Users\AMD-Ryzen\Documents\PY CODEX\3.1.py", line 20, in <module>
if x.isupper() == false:
AttributeError: 'list' object has no attribute 'isupper'
Here's my code:
#List of the movies
lst = ['Spidey', 'Castaway', 'Avengers', 'GI. JOE', 'Shallow']
#The data stored in this list will come from input("Name of movie") using .append
x=[]
print("Enter at least 5 of your favorite movies" "\n")
#Loop to repeat the same question 5 times
for i in range(5):
x.append(input("Name of movie:"))
#I used the set.intersection method to find the common elements between the two list
lst_as_set = set(lst)
intersection = lst_as_set.intersection(x)
intersection_as_lst = list(intersection)
if x.isupper() == false:
print("It will never work out. Nice meeting you!")
elif len(intersection_as_lst) == 3:
Ques = input("\n" "Do you love some of his movies?:")
if Ques == "yes":
print("\n" "You have", len(intersection_as_lst), "common fave movies and they are:")
print(intersection_as_lst)
elif Ques == "no":
print("It will never work out. I dont like")
s = set(x) - set(lst)
print(s)
elif len(intersection_as_lst) == 0:
Ques = input("Do you love some of his movies?:")
if Ques == "yes":
print("It will never work out. Nice meeting you!")
else:
print("It will never work out. Nice meeting you!")
CodePudding user response:
The error occurs because you are trying to apply a string method isupper()
to a list. You must use a loop with the parameter:
for c in x:
if not c[0].isupper():
print("It will never work out. Nice meeting you!")
break
CodePudding user response:
First in python it is False and not false. and as you want to stop the program you can raise an exception
x = list()
print("Enter at least 5 of your favorite movies\n")
for i in range(5):
m_name = input("Name of movie: ")
if m_name[0].islower():
raise 'must start with an uppercase letter'
x.append(m_name)
CodePudding user response:
You are checking if list is isupper . You'll need to do
output = []
for word in x:
if word[0].isupper() == False:
output.append(word)
print("It will never work out. Nice meeting you!")
CodePudding user response:
def ìs_properly_capitalized(word: str) -> bool:
if not word:
return False
return word[0].isupper() and not any([c.isupper() for c in word[1:]])
results = [ìs_properly_capitalized(word) for word in lst]
if False in results:
print("One or more words not properly capitalized")
sys.exit(1)