[英]How to do the program will terminate if your user input's first letter of a word is not capitalize?
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!")
You are checking if list is isupper .您正在检查 list 是否为isupper 。 You'll need to do你需要做
for word in x:
if word.isupper() == false:
print("It will never work out. Nice meeting you!")
break
First in python it is False and not f alse.首先在python中它是False而不是 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)
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.