首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如果您的用户输入的单词的第一个字母没有大写,如何做程序将终止?

如果您的用户输入的单词的第一个字母没有大写,如何做程序将终止?
EN

Stack Overflow用户
提问于 2021-12-01 07:51:25
回答 4查看 98关注 0票数 1

如何使此程序只接受用户输入,而遵循适当的大写。就像它不会接受“罗宾汉”除非是“罗宾汉”。当我运行它时,上面写着..。

代码语言:javascript
复制
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'

这是我的密码:

代码语言:javascript
复制
#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!")
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2021-12-01 08:20:30

发生此错误是因为您试图将字符串方法 isupper()应用于列表。必须对参数使用循环:

代码语言:javascript
复制
for c in x:
    if not c[0].isupper():
        print("It will never work out. Nice meeting you!")
        break
票数 1
EN

Stack Overflow用户

发布于 2021-12-01 08:12:09

您正在检查list是否为。你需要做的

代码语言:javascript
复制
output = []
for word in x:
    if word[0].isupper() == False:
        output.append(word)
        print("It will never work out. Nice meeting you!")
票数 1
EN

Stack Overflow用户

发布于 2021-12-01 08:14:45

在python中,首先是False,而不是false。当您想要停止程序时,可以引发异常。

代码语言:javascript
复制
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)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70180663

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档