简体   繁体   中英

How do I deal with Runtime Error in For Loop in Python?

Hi I am a Python beginner,

so I wanted to print a hallowed rectangle with *, and I wrote this:

m,n= int(input()), int(input())
for i in range(m):
    if (i==0 or i==m-1):
        print('*'*n)
    else:
        print('*'+" "*(n-2)+"*")

or either:

m,n= int(input()), int(input())

for i in range(m):
    for j in range(n):
      if (i==0 or i==m-1 or j==0 or j==m-1):
          print("*",end='')
      else:
          print(" ",end='')
    print() 

I don't know what was going wrong, but it kept telling me I've got runtime error:((

Both your code snippets work fine. The only time you can get a runtime error, judging by what you gave us at this moment, is when you enter letters instead of numbers for m and/or n .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM