Python Forum
multiply all input arguments if int or float
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
multiply all input arguments if int or float
#1
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
"""

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 total
All of the elements in list b are coming up as type str, how to convert them to int or float??
Reply
#2
Quote:Here is another assignment
If this is an assignment, it needs to be in the homework forum
Recommended Tutorials:
Reply
#3
I'm assuming literal_eval returns the float or int version of the string, or raises a value error if it can't. So what your program does is keep the inputs if they can be converted, but it's not keeping the converted value. So I would have solve return the result of literal_eval. If literal_eval raises an error, you just want to ignore that input. Well, if you're multiplying, ones are ignored. So I would return one if there's a value error.

Note that you're looping on the inputs twice. You loop through them in your list comprehension, checking each one with solve. Then in solve, you have a loop. But the loop is meaningless because it's always going to be exited on the first iteration with a return statement. There's no reason for the loop in solve, I would get rid of it.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#4
(Nov-03-2016, 07:50 PM)roadrage Wrote:       - If the argument is not a valid integer or float, it should be silently
      omitted from the calculation

so i can get around the requirement of 2 or more arguments by giving invalid ones like:

python multiply.py foo 6
if i were the teacher giving this assignment, i would require testing for the number of valid arguments being at least 2.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#5
(Nov-03-2016, 07:50 PM)roadrage Wrote: All of the elements in list b are coming up as type str, how to convert them to int or float??

As easily as asking nicely that they be int or float:
>>> x = '534'
>>> type(x)
<class 'str'>
>>> y = int(x)
>>> type(y)
<class 'int'>
>>> z = float(x)
>>> type(z)
<class 'float'>
That said, why are you using ast for anything this simple?  You're not creating blocks of code and traversing the call stack, you're just... multiplying numbers...
Speaking of which, why define a multiply function if you'll never call it?
Reply
#6
here is how i came up with the solution for this problem....


import sys
import re
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)]
h = []
for n in b:
    if n.isdigit():
        h.append(int(n))
    else:
        h.append(float(n))

print reduce(lambda c,d: c*d, h)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  python!TypeError: can't multiply sequence by non-int of type 'float' shaywune 2 9,515 Sep-24-2016, 04:33 PM
Last Post: shaywune

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020