我如何讓這個程式只接受在遵循正確大寫的情況下輸入的用戶輸入。就像它不會接受“羅賓漢”,除非它是“羅賓漢”。當我運行它時,它說...
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!")
uj5u.com熱心網友回復:
發生錯誤是因為您試圖將字串方法 isupper()應用于串列。您必須使用帶有引數的回圈:
for c in x:
if not c[0].isupper():
print("It will never work out. Nice meeting you!")
break
uj5u.com熱心網友回復:
首先在蟒蛇是假,而不是?F ALSE。并且當您想停止程式時,您可以引發例外
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)
uj5u.com熱心網友回復:
您正在檢查 list 是否為isupper。你需要做
output = []
for word in x:
if word[0].isupper() == False:
output.append(word)
print("It will never work out. Nice meeting you!")
uj5u.com熱心網友回復:
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)
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/371036.html
上一篇:從字典中獲取鍵串列與值串列匹配
