Python Forum
Temporarily storing the value of a function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Temporarily storing the value of a function
#1
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))
Reply
#2
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))
Reply
#3
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,
Reply
#4
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???
Reply
#5
Did you do python3.8.3 -m pip install numpy?
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#6
(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.
Reply
#7
@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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  temporarily buffer output, and output it later rowan_bradley 5 2,879 Mar-24-2021, 08:45 PM
Last Post: deanhystad
  Issues with storing variables outside of a function cerulean747 7 3,638 Apr-30-2020, 08:46 AM
Last Post: DeaD_EyE
  gnureadline: disable temporarily? klaymen 1 2,411 May-08-2018, 11:16 AM
Last Post: Larz60+
  Taking user input and storing that to a variable then storing that variable to a list jowalk 12 37,243 Mar-27-2017, 11:45 PM
Last Post: wavic

Forum Jump:

User Panel Messages

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