Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In python: starter code argv.py: import sys def print_block(width, height, symbol): # Remove the following line print ('print_block is not implemented') def print_args(): # Remove

In python:

image text in transcribed

image text in transcribed

starter code argv.py:

import sys

def print_block(width, height, symbol): # Remove the following line print ('print_block is not implemented')

def print_args(): # Remove the following line, obviously print ("print_args is not implemented")

def print_names(): print ("Flags:") # Remove the following line print ("print_flags is not implemented")

def parse_arguments(): # Remove the following line print ("parse_arguments is not implemented")

# Eventually, replace the following line with # return (width, height, symbol) return (2, 3, 'X')

def main(): print ('-----print_block----------------') print_block(5, 3, "W")

print ('-----print_args----------------') print_args()

print ('-----print_names--------') print_names()

print ('-----parse_arguments-----------') (width, height, symbol) = parse_arguments() print_block(width, height, symbol)

if __name__ == '__main__': main()

Goal: handle named and positional command-line arguments. We will work gradually towards this goal. Begin by using a text editor, or IDLE, to edit the file argv. py. Notice that you will NOT use IDLE to RUN the program. You will always run the program using the command line. 1. Implement the function print_args ( ) . Just write a for loop that travels through the list slice sys. argv[1:], and prints each entry on a separate line. We are just looking at the arguments, so we skip sys. argv[0], which is the program name "argv. py". Try it: go to the terminal, and type: python argv.py x-width 5 -height 3 CAVEAT: if you have a Mac, it comes with python 2 installed. This course uses python 3 . In order to get python 3 , type py thon 3 instead of python. Shortcut: on the terminal, you often want to repeat the last command. To do this, press the UP arrow (then maybe edit your command), and press ENTER. Try it! Instead of printing every argument, just print the names. First check if arg starts with a minus sign: if arg[]==1 : Then, instead of print (arg), discard the dash: print (arg [1:]). Go to the terminal and type the same thing again: python argv.py X-width 5 -height 3 3. Now you are ready to implement parse_arguments(). If you find a name, you have to access the argument ofter it. A for loop makes this awkward: a while loop is easier. Begin with this code: index =1 whtle index arg= sys.argv[index] index +=1 If arg is a name, you should - figure out if the name is "width" or "height". - increment index, and retrieve the next argument (which is a value). - remember to convert the value into an int! - change either the width or hei ght variable. If arg is not a name, then it's a positional argument. In this case, you should just store it into symbol. 4. If a user omits a parameter, it gets a default value. So, in parse_arguments (), give the three parameters width, height, and symbol the values 5, 3, and "X" initially (before you start the whtle loop). 5. At the end of parse_arguments (), return the three parameters, as a tuple: return (width, height, symbol) 6. Last step: implement print_block(). It gets passed the three parameters, and prints a block of symbols, with no spaces, and a " " after each line

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

AWS Certified Database Study Guide Specialty DBS-C01 Exam

Authors: Matheus Arrais, Rene Martinez Bravet, Leonardo Ciccone, Angie Nobre Cocharero, Erika Kurauchi, Hugo Rozestraten

1st Edition

1119778956, 978-1119778950

More Books

Students also viewed these Databases questions

Question

Give a brief defi ni tion of the terms population and sample.

Answered: 1 week ago

Question

14.4 Analyze in detail three basic causes of accidents.

Answered: 1 week ago