微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

我怎样才能摆脱 AttributeError 问题?

如何解决我怎样才能摆脱 AttributeError 问题?

当我将 python 文件上传到 codePost.io 时,出现“读取一行时出现 EOF 错误”的错误消息。我尝试使用“尝试和除外”方法,但在这种情况下,我得到了一个 AttributeError。我应该怎么办?这是我的代码

try:
    rows = int(input("Enter a number of rows: "))
    columns = int(input("Enter a number of columns: "))

    def print_rect(row,col):
      for r in range(1,row + 1):
        for i in range(1,col + 1):
          if r == 1 or r == row or i == 1 or i == col:
            print("*",end="")
          else:
            print(" ",end="")

        print()

    print_rect(rows,columns)
except EOFError:
    pass

解决方法

请试试这个:

try:
  print("Enter a number of rows:")
  rows = int(input())
  print("Enter a number of columns:")
  columns = int(input())

  def print_rect(row,col):
    for r in range(1,row + 1):
      for i in range(1,col + 1):
        if r == 1 or r == row or i == 1 or i == col:
          print("*",end="")
        else:
          print(" ",end="")

      print()

  print_rect(rows,columns)
 except EOFError:
    pass
 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。