简体   繁体   English

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

[英]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)

The error occurs because you are trying to apply a string method isupper() to a list.发生错误是因为您尝试将字符串方法isupper()应用于列表。 You must use a loop with the parameter:您必须使用带有参数的循环:

for c in x:
    if not c.isupper():
        print("It will never work out. Nice meeting you!")
        break
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.

相关问题 如何将 python 中每个单词的首字母大写? - How to capitalize the First letter of each word in python? 如何让我的代码将其中包含大写字母的单词的首字母大写? (猪拉丁语) - How do I make my code capitalize the first letter of the word that has a capital letter in it? (Pig Latin) 如何仅大写列表中第一个单词的第一个字母 - How to capitalize only first letter of first word in a list Python如何将单词的第一个字母和后三个字母大写 - Python how to capitalize first letter of a word and last three letters 如何将字符串中每个单词的首字母大写? - How can I capitalize the first letter of each word in a string? Python中列表中第一个单词的首字母大写 - Capitalize first letter of the first word in a list in Python 如何将用户输入的字符串的第一个字母大写,但保留字符串大写的 rest - How to capitalize the first letter of a user-inputted string but keep the rest of the string's capitalization 如何在python中将每个单词的第一个字母大写而不使用.capitalize or.upper or.title - how to capitalize first letter of every word without .capitalize or .upper or .title in python 将Python中的每个单词的首字母大写 - Capitalize first letter of each word in the column Python 输出用户输入的每个单词的第一个字母 - Output first letter of each word of user input
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM