如何使此程序只接受用户输入,而遵循适当的大写。就像它不会接受“罗宾汉”除非是“罗宾汉”。当我运行它时,上面写着..。
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'这是我的密码:
#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!")发布于 2021-12-01 08:20:30
发生此错误是因为您试图将字符串方法 isupper()应用于列表。必须对参数使用循环:
for c in x:
if not c[0].isupper():
print("It will never work out. Nice meeting you!")
break发布于 2021-12-01 08:12:09
您正在检查list是否为和。你需要做的
output = []
for word in x:
if word[0].isupper() == False:
output.append(word)
print("It will never work out. Nice meeting you!")发布于 2021-12-01 08:14:45
在python中,首先是False,而不是false。当您想要停止程序时,可以引发异常。
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)https://stackoverflow.com/questions/70180663
复制相似问题