Question

Please answer this question using python programming only and provide a screen short for this code....

Please answer this question using python programming only and provide a screen short for this code.

Homework 3 - Multiples not less than a number

Question 1 (out of 3)

This homework is a SOLO assignment. While generally I encourage you to help one another to learn the material, you may only submit code that you have composed and that you understand.

Use this templatePreview the document to write a Python 3 program that calculates the nearest multiple of a number that is not less than a factor. Your program should get two arguments from the command line and pass them to a function called get_nearest_multiple() -- this part has been written for you. You need to write a function get_nearest_multiple() that takes two integer arguments, in the following order: a minimum number and a factor. Your function should return an integer that is the smallest multiple of the factor that is not less than the minimum number. You can assume that the minimum number will not be negative and that the factor will be positive.

Special requirements

  • use the provided templatePreview the document
  • write a function called get_nearest_multiple() that takes two arguments and returns an int, as described above
  • get_nearest_multiple()should not call input() or print()
  • (not actually a special requirement, but be sure your program adheres to the style guide)

Sample output

Below is a sample of what your Python script's output might look like.

$ python3 abills-inst326-homework3-q1.py 15 10
20

Here are a few more test cases:

assert get_nearest_multiple(0, 1) == 0
assert get_nearest_multiple(15, 7) == 21
assert get_nearest_multiple(435, 10) == 440
assert get_nearest_multiple(435, 200) == 600
assert get_nearest_multiple(10, 435) == 435
assert get_nearest_multiple(200, 435) == 435

Below is the copy of the template we are ask to use from the instruction above.

# your header here

"""Your docstring here"""

import argparse

import sys

# your function(s) here

def parse_args(arglist):

"""

Process command-line arguments.

Args:

arglist (list of str): arguments passed to the script from the

command line.

Returns:

args (obj): an object with attributes min_val (int) and

factor (int).

Raises:

argparse.ArgumentError: if min_val is negative or factor is less

than one.

Side effects:

if arglist contains '-h', parser.parse_args() will print a help

statement and exit.

if arglist does not match the arguments expected by the argument

parser, parser.parse_args() will print a short usage

statement and exit.

"""

# set up the argument parser

parser = argparse.ArgumentParser(

description="Given two values, min_val and factor, find the smallest"

" value >= min_val that is a multiple of factor."

)

parser.add_argument('min_val', type=int,

help='result should be no less than this number')

parser.add_argument('factor', type=int,

help='return value should be a multiple of this number')

# parse the command-line arguments

args = parser.parse_args(arglist)

# validate arguments

if args.min_val < 0:

raise argparse.ArgumentError('min_val must be greater than 0')

if args.factor < 1:

raise argparse.ArgumentError('factor must be greater than 1')

# return arguments

return args

if __name__ == '__main__':

args = parse_args(sys.argv[1:])

nearest_multiple = get_nearest_multiple(args.min_val, args.factor)

print(nearest_multiple)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

'''
Python version : 3.6
Python program to create a function to get nearest multiple
'''

import argparse
import sys

# function that returns the smallest multiple of factor starting from min_val
def get_nearest_multiple(min_val, factor):
  
   i = min_val # set i to min_val
  
   # loop that continues till we get the multiple
   while True:
       if( i%factor == 0): # if i is divisible by factor, return i
           return i
       i += 1 # increment i
      
  


def parse_args(arglist):
   """
   Process command-line arguments.
   Args:
   arglist (list of str): arguments passed to the script from the
   command line.
   Returns:
   args (obj): an object with attributes min_val (int) and
   factor (int).
   Raises:
   argparse.ArgumentError: if min_val is negative or factor is less
   than one.
   Side effects:
   if arglist contains '-h', parser.parse_args() will print a help
   statement and exit.
   if arglist does not match the arguments expected by the argument
   parser, parser.parse_args() will print a short usage
   statement and exit.
   """
      
   # set up the argument parser
   parser = argparse.ArgumentParser(description="Given two values, min_val and factor, find the smallest value >= min_val that is a multiple of factor.")
   parser.add_argument('min_val', type=int,help='result should be no less than this number')
   parser.add_argument('factor', type=int,help='return value should be a multiple of this number')
   # parse the command-line arguments
   args = parser.parse_args(arglist)
   # validate arguments
   if args.min_val < 0:
       raise argparse.ArgumentError('min_val must be greater than 0')
   if args.factor < 1:
       raise argparse.ArgumentError('factor must be greater than 1')
   # return arguments
   return args

if __name__ == '__main__':
   args = parse_args(sys.argv[1:])
   nearest_multiple = get_nearest_multiple(args.min_val, args.factor)
   print(nearest_multiple)  
   assert get_nearest_multiple(0, 1) == 0
   assert get_nearest_multiple(15, 7) == 21
   assert get_nearest_multiple(435, 10) == 440
   assert get_nearest_multiple(435, 200) == 600
   assert get_nearest_multiple(10, 435) == 435
   assert get_nearest_multiple(200, 435) == 435
  
#end of program

Code Screenshot:

1 Python version 3.6 Python program to create a function to get nearest multiple 2 4 5 import argparse import sys 7 # functio

22 Edef parse_args (arglist) 23 24 Process command-line argument.s. 25 26 Args: arglist (list of str) arguments passed to the

56 main 57 Eif name args parse_args (sys.argv [1:1) nearest multiple print (nearest_multiple) assert get_nearest_multiple (0,

Add a comment
Know the answer?
Add Answer to:
Please answer this question using python programming only and provide a screen short for this code....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • This is for programming using Python with the JES software. Please write the code with the...

    This is for programming using Python with the JES software. Please write the code with the proper indentation. Thanks! Problem 1: Write a function that takes 2 arguments (integer or float) and computes the sum, difference, product, and quotient of the arguments. Once you calculate the values you should print out statements telling the user what arguments we used to compute the values and what the result of each calculation (your function should produce 5 lines of output in the...

  • Please write this code in python programming and provide a screen short of the code. Reading:...

    Please write this code in python programming and provide a screen short of the code. Reading: P4E 7; Tut 7.2, 7.2.1 Upload an original Python script that satisfies the following criteria: It has at least two functions Function 1: This function has one parameter, a string containing the path to an existing text file that will be read by your function Using a with statement, the function opens the file indicated by the parameter for reading The function counts the...

  • Python help! Any help is appreciated, thank you! Fill in the missing function, monthString(), in the...

    Python help! Any help is appreciated, thank you! Fill in the missing function, monthString(), in the program. The function should take number between 1 and 12 as a parameter and returns the corresponding month as a string. For example, if the parameter is 1, your function should return "January". If the parameter is 2, your function should return out "February", etc. def monthString(monthNum): """ Takes as input a number, monthNum, and returns the corresponding month name as a string. Example:...

  • *PYTHON EXPERTS ONLY PLEASE* Please provide the answer coded in Python using comments to explain code...

    *PYTHON EXPERTS ONLY PLEASE* Please provide the answer coded in Python using comments to explain code function. Good answers will be rated with thumbs up! Please answer both parts. Thank you for your time. Question 3 - Suppose you have a file named numbers.csv which contains a bunch of integers, five per line of text, separated by commas. Write code below that will open the _le, read the numbers from it, and print the sum of all the even numbers...

  • The original code using the gets() function is written below. You need to do (a) change...

    The original code using the gets() function is written below. You need to do (a) change the provided code so that you now use fgets() function to obtain input from the user instead of gets(), (b) make any other necessary changes in the code because of using fgets() function, and (c) fill in the code for the execute() function so that the whole program works as expected (a simple shell program). Note: part c is already done, and the execute...

  • I am writing python code. I submitted an assignment but the professor said it was a...

    I am writing python code. I submitted an assignment but the professor said it was a modularization. Can you help me see what part of the code is modularization? Pseudocode: 1. Decalre MainMethod () function: # A. Parameters: Three numbers # B. Put the three numbers in reverse # 2. Main Program # A. Initialize Varibles # B. Accepts three values from the user # C. Invoke MainMethod () function, passing the three numbers as arguments in reverse # D....

  • Need help with Python (BinarySearch), code will be below after my statements. Thank you. Have to...

    Need help with Python (BinarySearch), code will be below after my statements. Thank you. Have to "Add a counter to report how many searches have been done for each item searched for." Have to follow this: 1) you'll create a counter variable within the function definition, say after "the top = len(myList)-1" line and initialize it to zero. 2) Then within the while loop, say after the "middle = (bottom+top)//2" line, you'll start counting with "counter += 1" and 3)...

  • Write a Python program that reads user input from the command-line. The program should define a...

    Write a Python program that reads user input from the command-line. The program should define a function read Position, which reads the values for t, v0, and h0. If there is an IndexError, print 'Please provide the values for t, vO, and hO on the command line.'. If t, v0, or h0 are less than 0. print 't = # is not possible.' for each variable respectively. Note that the # represents the number entered on the command-line by the...

  • Python Chet is trying to create a tail program for his windows machine so he can...

    Python Chet is trying to create a tail program for his windows machine so he can see the last N lines of a file or list of files. He wants the program to be usable by his friends and family so when an argument does not get passed to the program it should display a usage message. This is shown by the parser object by default but it's not doing it for him. The code works just not this one...

  • Write a full program that will accept any number of command line arguments and print them...

    Write a full program that will accept any number of command line arguments and print them to the screen. Suppose your program is called Print.java. When the command java Print hello goodbye you is run, the output will be hello goodbye you Write a full program that will accept exactly  three command line arguments that can be interpreted as integers, and will add them up. If there aren't exactly 3 command line arguments, print an error message and terminate the program....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT