Python Forum

Full Version: Temporarily storing the value of a function
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello everyone,

I'm new here and a newbie to python, my heart is not on my sleeve so whatever comments made i'm cool with it.

What I'm trying to do below is get the user's input, which will supposedly fill in the variables then calculate what needs to be calculated. The problem is when i run it, it asks for one particular value twice ( def BOM())because of the formula. What i actually would like to happen is once the user types in the requested value, that the value be automatically replicated to wherever the function is being called. Don't know how possible that is.

Oh! by the way I'm using python 2.X

Thanks in advance guys.
Cheers,

def BOM():
    bm = []
    while True:
        l_a = int(raw_input("BOM per item: "))
        if l_a == "done":break
        bm.append(l_a)
        bm = np.asarray(bm)
        return bm

def scrap():
    scrp = []
    while True:
        l_b = int(raw_input("Scrap percentage per item: "))
        if l_b > 1:
            l_b = l_b / float(100)
            scrp.append(l_b)
            return l_b

wd_kg_itm = (BOM() * scrap()) + BOM()
print("Wood kg per item is {0}".format(wd_kg_itm))
FYI (and you probably know this), Python 2.x is all but dead, so the sooner you can convert to 3.x the better.

Your code seems to be incomplete, but I assume you are importing numpy as np before running this? You can assign the return value from your BOM() function to a variable so that you can use it multiple times without prompting for input more than once.

b = BOM()
wd_kg_itm = (b * scrap() + b)
print("Wood kg per item is {0}".format(wd_kg_itm))
K...I'll take your advice and upgrade to 3.x.

Thanks for the quick response, your suggestion solved my problem.
I imported numpy as np in order for me to readily use it.

Thanks again.
Cheers,
Hey GOTO10...took your advice and upgraded to Py3.8.3 now the problem is numpy is not working.

Saying: ModuleNotFoundError: No module named 'numpy'

Where as the module is already installed. To make sure of that I pip installed the latest version
of numpy again and i got:

Requirement already satisfied: numpy in /Users/vm/anaconda3/lib/python3.7/site-packages (1.16.2)

What am I doing wrong???
Did you do python3.8.3 -m pip install numpy?
(Jun-21-2020, 02:56 PM)Men Wrote: [ -> ]Hey GOTO10...took your advice and upgraded to Py3.8.3 now the problem is numpy is not working.

Requirement already satisfied: numpy in /Users/vm/anaconda3/lib/python3.7/site-packages (1.16.2)

Versions don't match install to Py3.8.3 as menator01 said.
@Menator01: No, I didn't do that. I just went to the numpy site and copied the pip install file and pasted it in the atom terminal and it installed.

@GOTO10: atom is pointing to that directory but when I check the --version it tells me 3.8.3 is installed:

(base) My-MacBook-Pro:Python_Folder vm$ python3 --version
Python 3.8.3
(base) My-MacBook-Pro:Python_Folder vm$

@menator01 & @GOTO10:

I did the pip install in the manner suggested and it worked. apparently my pip installer was outdated and I managed to upgrade it...everything is working fine now. Thx. Cheers.

I meant to refer to Yoriz and not GOTO10. Apologies.