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?)

Ks1999
  • 13
  • 6
  • 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. – Ahmed AEK Sep 18 '22 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. – tygzy Sep 18 '22 at 18:43

1 Answers1

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)
bitflip
  • 3,436
  • 1
  • 3
  • 22
  • 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. – Ks1999 Sep 18 '22 at 19:13
  • you can use the break keyword to exit the while loop. Just add break in a new line – bitflip Sep 18 '22 at 19:17
  • Another question, what if the whole is the whole name_=="main": doing in the code? – Ks1999 Sep 18 '22 at 19:24
  • Somebody else was better answering that ;) https://stackoverflow.com/questions/419163/what-does-if-name-main-do – bitflip Sep 18 '22 at 19:26
  • 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? – Ks1999 Sep 18 '22 at 20:14
  • I would either add a "name" in the PRODUCTS dictionary. Or you can save your input in a separate variable. `product_name = input("Enter product name: ") product = PRODUCTS.get(product_name) print(product_name)` – bitflip Sep 18 '22 at 20:19
  • Thx man, your a damn legend :) – Ks1999 Sep 18 '22 at 20:25