Nov-03-2016, 07:50 PM
Here is another assignment problem, I'm working on...
"""
Write a script that:
1. When execuetd via the command line will:
1.1. Print "Not enough arguments", if it is ran with less than 2 arguments
1.2. Print the product of all supplied arguments, keeping in mind that:
- If the argument is a valid integer, then it should be converted to an
integer type
- Else, if the argument is a float number containing a decimal dot, it
should be converted to a float type
- If the argument is not a valid integer or float, it should be silently
omitted from the calculation
2. Print nothing when imported as a python module
Examples:
python multiply.py 1 2 3 4
24
python multiply.py 1.0 2 3 4
24.0
python multiply.py 0.9 2 3 4
21.6
python multiply.py foo 2 3 4
24
python multiply.py 1
Not enough arguments
"""
"""
Write a script that:
1. When execuetd via the command line will:
1.1. Print "Not enough arguments", if it is ran with less than 2 arguments
1.2. Print the product of all supplied arguments, keeping in mind that:
- If the argument is a valid integer, then it should be converted to an
integer type
- Else, if the argument is a float number containing a decimal dot, it
should be converted to a float type
- If the argument is not a valid integer or float, it should be silently
omitted from the calculation
2. Print nothing when imported as a python module
Examples:
python multiply.py 1 2 3 4
24
python multiply.py 1.0 2 3 4
24.0
python multiply.py 0.9 2 3 4
21.6
python multiply.py foo 2 3 4
24
python multiply.py 1
Not enough arguments
"""
import sys from ast import literal_eval if len(sys.argv)<3: sys.exit("Not Enough Arguments") def solve(lis): for x in lis: try: literal_eval(x) return True except ValueError: return False a = sys.argv b = [z for z in a if solve(z)] def multiply(h): total = 1 for g in h: total *= g return totalAll of the elements in list b are coming up as type str, how to convert them to int or float??