1

There are multiple variables that I'm trying call in multiple parts of the code.

  1. Firstly I need to call stock in num_loop from product_loop
  2. Next would be productprice from product_loop and num from num_loop in the final line of the code
def product_loop(): 
   product = input ("Enter product name: ")
   if product == 'apple':
       productprice = 3.5
       stock = 134
   elif product == 'banana':
       productprice = 6.82
       stock = 52    
   else:
       print ("Sorry, no such product. Try again.")
       #loops back to the start of product input
       product_loop()
       
def num_loop():
   num = int(input ("Enter number of products ordered: "))
   if num>stock:        #trying to call stock from product_loop
       print ("Sorry, not enough stock. Try again.")
       num_loop()

totalprice=productprice*num       #trying to call productprice from product_loop and num from num_loop
print(totalprice)

Would I need to use the return statement or a class to accomplish this?
*Edit(How would I use a return statement in this situation?)

2
  • you don't need a class if you add global stock, productprice, num at the start of every function, adding a class or return is just a matter of writing better maintainable and readable and scalable code, and i don't see any function calling any other function or any function being called, so i don't know how your program flows. Commented Sep 18, 2022 at 18:42
  • You could just define the variables you want to be shared across functions on the global scope, meaning the functions are defined outside of any function. totalprice is a global variable as an example. Commented Sep 18, 2022 at 18:43

1 Answer 1

1

Yes, usually you would write a function that returns a product with its information. If you dont want to use that in this basic example, you can also do something like that. Store all product information in a dictionary and use .get() to access the parameters.

PRODUCTS = {
    "apple": {"price": 3.5, "stock": 134},
    "banana": {"price": 6.82, "stock": 52}
}


if __name__ == "__main__":
    while True:
        product = PRODUCTS.get(input("Enter product name: "))
        if not product:
            print("Sorry, no such product. Try again.")
            continue

        num = int(input("Enter number of products ordered: "))
        stock = product.get("stock", 0)
        if num > stock:
            print("Sorry, not enough stock. Try again.")
            continue

        price_per_unit = product.get("price", 0)
        print("Ordervalue: ", price_per_unit * num)
Sign up to request clarification or add additional context in comments.

7 Comments

How would I stop the second part from continuing after getting the total price of a products? Cause your code here just loops around once it displays the price.
you can use the break keyword to exit the while loop. Just add break in a new line
Another question, what if the whole is the whole name_=="main": doing in the code?
Somebody else was better answering that ;) stackoverflow.com/questions/419163/what-does-if-name-main-do
Using this code, how would I be able to print the product variable that I put in the input? For example, if I inputted apple, how would I print apple at the end of the code?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.