【问题标题】:Write a program that reads in a string and prints whether it编写一个程序,读取一个字符串并打印它是否
【发布时间】:2020-09-07 03:56:08
【问题描述】:
  • 仅包含字母
  • 仅包含大写字母
  • 仅包含小写字母
  • 仅包含数字
  • 仅包含字母和数字
  • 以大写字母开头
  • 以句点结尾

words = input("Enter a Letter: ")
int_words = int(words)
if words.isupper() == words.islower():
    print("Contains only letters")
elif words.isupper() > words.islower():
    print("Contains only upper letters")
else:
    print("Contains only lower letters")
if int_words.isdigit():
    print("Contains only digits")
else:
    print("Contains Letters and Digits")
我的任务还没有完成一半,我已经收到了这个错误。在打印(“仅包含数字”)中,我希望它能够在“仅包含数字”上运行,但它同时运行“包含字母”和“包含数字”。 我也收到此错误

"AttributeError: 'int' 对象没有属性 'isdigit'" 我只想将字符串转换为 int 以便我只能运行 print("Contains only Digits")

我真的需要帮助。我是编码方面的新生,所以我对此很陌生。 :(

【问题讨论】:

    标签: python xcode


    【解决方案1】:

    在下面的代码示例中,我首先评估了字符串(单词)是否为数字,如果不是,则继续使用 for 循环检查字符串是否包含数字,您可以阅读有关 for 循环的更多信息在 [here][1] 和 [here][2] 对于脚本的其余部分,我刚刚评估了使用 isis not 满足的任何其他条件 em> 比较语句,您可以在 [此处][3] 中阅读更多相关信息,然后继续打印所需的输出。

    words = input("Enter a Letter: ")
    numbers = False
    if not words.isdigit():
        for character in words:
            if character.isdigit():
                print("Contains Letters and Digits.")
                numbers = True
                break
    if not words.isdigit() and words.islower() and not words.endswith(".") and not numbers:
        print("Contains only letters.")
        print("Contains only lower case letters.")
    elif not words.isdigit() and words.islower() and words.endswith(".") and not numbers:
        print("Contains only letters.")
        print("Contains only lower case letters.")
        print("Ends with period.")
    elif not words.isdigit() and words.isupper() and not words.endswith(".") and not numbers:
        print("Contains only letters.")
        print("Contains only upper letters.")
    elif not words.isdigit() and words.isupper() and words.endswith(".") and not numbers:
        print("Contains only letters.")
        print("Contains only upper letters.")
        print("Ends with period.")
    elif words.isdigit():
        print("Contains only digits.")```
    
    
      [1]: https://realpython.com/python-for-loop/
      [2]: https://www.w3schools.com/python/python_for_loops.asp
      [3]: https://realpython.com/python-is-identity-vs-equality/
    

    【讨论】:

      【解决方案2】:

      这应该可以帮助您入门:

      lstupper = [chr(x) for x in range(65,90+1)]  # A-Z
      lstlower = [chr(x) for x in range(97,122+1)]  # a-z
      lstnums =  ['0','1','2','3','4','5','6','7','8','9']
      
      lstletters = lstupper + lstlower
      lstlettersandnums = lstupper + lstlower + lstnums
      
      word = 'apple'
      
      allletters  = True
      allnums = True
      
      for c in word:  # each letter in 'apple'
         if not c in lstnums:  # if character not in number list
             allnums = False  
         if not c in lstletters:  # if character not in letter list
             allletters = False
             
      print(allnums, allletters)   # False True
      

      【讨论】:

        【解决方案3】:

        这样就解决了这个问题:

        words = input("Enter a Letter: ")
        if words.isalpha():
            print("Contains only letters")
        elif words.isupper():
            print("Contains only upper letters")
        elif words.islower():
            print("Contains only lower letters")
        elif words.isalnum():
            print("Contains only digits")
        

        【讨论】:

          猜你喜欢
          • 2020-02-28
          • 2021-04-12
          • 1970-01-01
          • 2018-11-19
          • 2012-01-13
          • 2020-01-22
          • 2020-07-27
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多