我怎样才能摆脱 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