CS298 / EDUC298
Fall 2023
Stanford University
Computer Science Department
Lecturer: Chris Gregg
python rectangle.py 4 7
draws the following rectangle:myth51$ python rectangle 4 7
****
* *
* *
* *
* *
* *
* *
****
myth51$
#!/usr/bin/env python3
import sys
def drawRec(w, h):
SYMBOL = "*"
if w <= 0 or h <= 0:
print("Both sides of a rectangle must be positive!")
return
topAndBottom = SYMBOL * w
middle = SYMBOL + ' ' * (w - 2) + SYMBOL
print(topAndBottom)
for i in range(h - 2):
print(middle)
print(topAndBottom)
if __name__ == "__main__":
if len(sys.argv) != 3:
print("Usage: {} width height".format(sys.argv[0]))
quit()
try:
drawRec(int(sys.argv[1]),int(sys.argv[2]))
except ValueError:
print("Both parameters must be positive integers.")