Unit 18: Procedural Programming Assignment (Shop Simulation) - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: Unit 18: Procedural Programming Assignment (Shop Simulation) (/thread-6992.html) |
Unit 18: Procedural Programming Assignment (Shop Simulation) - shaheduk323 - Dec-16-2017 Unit 18: Procedural Programming Assignment (Shop Simulation) Specification The company are involved in several innovative projects and are currently in the process of obtaining a contract for a game that simulates the financial running of a shop that sells a single product. What you have been asked to do is develop and implement a prototype that demonstrates the basic principle of such a game using the process detailed below: Step 1: The user is presented with an initial balance. Step 2: The user enters how much they want to spend on advertising their product (as this is a prototype, the shop only sells one item), and how many product items they wish to purchase for their stock. A new balance is calculated based on the number of product items purchased and how much has been spent on advertising. The wholesale purchase price and the shop’s sales price of the product should be set as constants in the program. Step 3: The simulation then runs for 30 days. Each day, a random number must be generated to calculate sales figures by multiplying the random number with how much has been spent on advertising. A new balance is calculated based on sales with a deduction of daily expenses. Each day (one second) data for stock, sales, balance and the day number are displayed to the user. Step 4: Step 2 is repeated until the program is closed. Other Information: If the shop runs out of stock before the end of the 30 days, they cannot make any more sales until the 30 days are over and more stock is purchased. If the balance goes below zero, the simulation ends. My Code initialbalance = 500 wholesale = 20 import random # "Procedures used in the program" def balance(): print ("the initial balance is £ " +str(totalmoney)) return balance def numofproduct(): productname = int(input("How many product items do you wish to purchase? ")) return numofproduct def newbalance(): newbalance = initialbalance - (productname * advertcost) return newbalance def advertisingcost(): advertcost = int(input("How much do you want to spend on advertising cost in £? ")) def randmnum(): randmnum = random.randint(1,100) # "30 Days Simulation loop" days = 0 while days <30: days = days +1 print("days", + (days)) randmnum = random.randint(1,100) figuresales = (randmnum * advertisingcost) print ("The figure sales is £" +str (figuresales)) Error!! Traceback (most recent call last): File "C:\Users\Shahed\Desktop\Python\Portable Python 3.2.1.1\assignment python.py", line 42, in <module> figuresales = (randmnum * advertisingcost) TypeError: unsupported operand type(s) for *: 'int' and 'function' >>> I do not understand what the error means and cannot find a way to fix this RE: Unit 18: Procedural Programming Assignment (Shop Simulation) - j.crater - Dec-16-2017 Hi, please edit your post so that code is put in Python code tags and error message in error code tags. You can find help here. RE: Unit 18: Procedural Programming Assignment (Shop Simulation) - shaheduk323 - Dec-16-2017 initialbalance = 500 wholesale = 20 import random # "Procedures used in the program" def balance(): print ("the initial balance is £ " +str(totalmoney)) return balance def numofproduct(): productname = int(input("How many product items do you wish to purchase? ")) return numofproduct def newbalance(): newbalance = initialbalance - (productname * advertcost) return newbalance def advertisingcost(): advertcost = int(input("How much do you want to spend on advertising cost in £? ")) def randmnum(): randmnum = random.randint(1,100) # "30 Days Simulation loop" days = 0 while days <30: days = days +1 print("days", + (days)) randmnum = random.randint(1,100) figuresales = (randmnum * advertisingcost) print ("The figure sales is £" +str (figuresales))
RE: Unit 18: Procedural Programming Assignment (Shop Simulation) - j.crater - Dec-16-2017 Nice job with code tags. Problem is that randnum is an integer variable, but advertisingcost (without parentheses!!!) is a function object. So error message simply tells you that you can't multiply integer and a function (you don't say?! :P). What you probably want instead of advertisingcost is the value that the function returns. For that, you will need to call that function (function name with parenthses) and modify that function, so it returns the value you want to include in calculation. I see you did that in your other functions. RE: Unit 18: Procedural Programming Assignment (Shop Simulation) - shaheduk323 - Dec-16-2017 Quote:What you probably want instead of advertisingcost is the value that function returns. For that, you will need to call that function (function name with parenthses) and modify that function, so it returns the value you want to include in calculation. i dont understand still. what can i modify this into RE: Unit 18: Procedural Programming Assignment (Shop Simulation) - j.crater - Dec-16-2017 Modify the function so it will return a value. That way when you will be able to multiply randnum with another number - the one your function (advertisingcost) returns. RE: Unit 18: Procedural Programming Assignment (Shop Simulation) - shaheduk323 - Dec-16-2017 Quote:Modify the function so it will return a value. That way when you will be able to multiply randnum with another number - the one your function (advertisingcost) returns. i cannot get it to return a value because this is from the users input of the (advertising cost) on line 22 & 23
RE: Unit 18: Procedural Programming Assignment (Shop Simulation) - j.crater - Dec-16-2017 User inputs a value, that is stored into a variable. And you can return the value of that variable from a function, as you did in other functions. RE: Unit 18: Procedural Programming Assignment (Shop Simulation) - shaheduk323 - Dec-16-2017 (Dec-16-2017, 08:36 PM)j.crater Wrote: User inputs a value, that is stored into a variable. And you can return the value of that variable from a function, as you did in other functions.i did that and it still shows the same error. i did return advertising cost def newbalance(): newbalance = initialbalance - (productname * advertcost) return newbalance def advertisingcost(): advertcost = int(input("How much do you want to spend on advertising cost in £? ")) return advertisingcost def randmnum(): randmnum = random.randint(1,100) # "30 Days Simulation loop" days = 0 while days <30: days = days +1 print("days", + (days)) randmnum = random.randint(1,100) figuresales = (randmnum * advertisingcost()) print ("The figure sales is £" +str (figuresales)) still the same error
RE: Unit 18: Procedural Programming Assignment (Shop Simulation) - j.crater - Dec-16-2017 You returned the function, not the value (of variable) which the function calculated (advertisingcost vs. advertcost, big difference!) |