Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
loop issues
#1
import random #this is to import the random number for calculations
initialBalance = 1000 #this is the initial balance £1000
wholeSalePrice = 10 #this is the wholesale price
shopSalePrice = 40 #shops sells the product for £40
addvertCost = 1,20 #advertising cost is between the range of £1-£20
days = 0
newBalance = 0
#procedures

def costs (wholeSalePrice,productPurchased):
calculations = wholeSalePrice*productPurchased #this is to calculte the wholeslae price by the product purchased price
print (calculations)
return calculations

def calcNewBalance (newBalance,addvertCost,productPurchased): #this is to pass the calculations throgh the parameter
answer = newBalance-addvertCost-productPurchased #this is to output the new balnace after deducting the advertising cost and the product purchased cost from the new balnace
print (answer)
return answer


def calculateSalesFigures(salesFigures): #this is to generate a number to calculate sales on a daily basis
sales = random.randint(1,50)
print (sales)
return sales

def expenses(everyDayExpenses): #this is to calculate the daily expenses
WholeSalePrice = 4
stock = 5
dailyExpenses = WholeSalePrice*stock
print (dailyExpenses)
return dailyExpenses

def balanceUpdate(newBalance,salesFigure,dailyExpenses):
updatedBalance = (newBalance + salesFigure - dailyExpenses)#this is the remaining balance after the daily expenses
print ("newbalance is £:" + str(updatedBalance))
print (updatedBalance)
return updatedBalance



#input

itemPurchased = int(input("please enter the value for itempurchased £")) #this inforamtion will be displayed for the user to insert the amount
advertisingCost = int(input("please enter a value for addvertcost £")) #this inforamtion will be displayed for the user to insert the amount
productPurchased = int (input ("please enter the amount of products you wish to purchase for stock £")) #this inforamtion will be displayed for the user to insert the amount
advertCost = int(input("Please enter how much you want to spend on advertising £"))

print ("")


#process

while days>0:
days = days + 1
print ("days" + str (days)) #this is the while statement for the loop

randomNumber = random.randint(1,50)
salesFigure = (randomNumber*advertisingCost)
print ("salesFigures is £:" +str(salesFigure)) #this block of code is to work out the sales figure by random number*advertising cost

updatedBalances = (newBalance + salesFigure - dailyexpenses) #this is to display the newbalnce on the users screen
print ("newbalance is £:" + str(updatedBalances))
print ("")

sold = salesFigure // priceOfProduct - shopPrice
if (sold > shopPrice):
lackOfStock = sold - stock
costProfit = lackOfStock*priceOfProduct #this is a block of code for If statement showing working out for handling products sold and in stock
sales = sales - costProfit


#output

if newBalance >=0:
print ("stock is fine ")
else:

print("we need more stock") #this IF statement is to decide if the stock is okay or more stock is needed



costs = (wholeSalePrice,productPurchased) #these are thevariables set for procedures
newBalance = calcNewBalance(newBalance,advertCost,productPurchased)
salesFigure = calculateSalesFigures(0)
dailyExpenses = expenses(0)
updatedBalances = balanceUpdate(newBalance,salesFigure,dailyExpenses)
Error:
please enter the value for itempurchased £50 please enter a value for addvertcost £4 please enter the amount of products you wish to purchase for stock £6 Please enter how much you want to spend on advertising £2 -8 26 20 newbalance is £:-2 -2 >>>
Reply
#2
Can you write a description of the problem, and what the result should be instead?
The error block doesn't look like a Python error message, there is even >>> , meaning it waits for input.
Reply
#3
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.
Reply
#4
days = 0
while days > 0:
  ...
The while-loop is flagged false (0>0 is false) and skip the code in the while loop

days = 1
while days>0:
  days = days + 1
  ...
This would run infinitely

days = 0
while days<30:
  days = days + 1
  ...
This would run 30 times
When my code doesn't work I don't know why **think** and when my code works I don't know why **think**
Reply
#5
It is hard to read your code as you pasted it into the BBS tags incorrectly and lost the formatting (indentation).

I'm guessing that the code should look like this (but I might have indented some things I shouldn't have):
import random #this is to import the random number for calculations
initialBalance = 1000 #this is the initial balance £1000
wholeSalePrice = 10 #this is the wholesale price
shopSalePrice = 40 #shops sells the product for £40
addvertCost = 1,20 #advertising cost is between the range of £1-£20
days = 0
newBalance = 0
#procedures
 
def costs (wholeSalePrice,productPurchased):
    calculations = wholeSalePrice*productPurchased #this is to calculte the wholeslae price by the product purchased price
    print (calculations)
    return calculations
 
def calcNewBalance (newBalance,addvertCost,productPurchased): #this is to pass the calculations throgh the parameter
    answer = newBalance-addvertCost-productPurchased #this is to output the new balnace after deducting the advertising cost and the product purchased cost from the new balnace
    print (answer)
    return answer
 
 
def calculateSalesFigures(salesFigures): #this is to generate a number to calculate sales on a daily basis
    sales = random.randint(1,50)
    print (sales)
    return sales
 
def expenses(everyDayExpenses): #this is to calculate the daily expenses
    WholeSalePrice = 4
    stock = 5
    dailyExpenses = WholeSalePrice*stock
    print (dailyExpenses)
    return dailyExpenses
 
def balanceUpdate(newBalance,salesFigure,dailyExpenses):
    updatedBalance = (newBalance + salesFigure - dailyExpenses)#this is the remaining balance after the daily expenses
    print ("newbalance is £:" + str(updatedBalance))
    print (updatedBalance)
    return updatedBalance
 
 
 
#input
 
itemPurchased = int(input("please enter the value for itempurchased £")) #this inforamtion will be displayed for the user to insert the amount
advertisingCost = int(input("please enter a value for addvertcost £")) #this inforamtion will be displayed for the user to insert the amount
productPurchased = int (input ("please enter the amount of products you wish to purchase for stock £")) #this inforamtion will be displayed for the user to insert the amount
advertCost = int(input("Please enter how much you want to spend on advertising £"))
 
print ("")
 
 
#process
 
while days>0:
    days = days + 1
    print ("days" + str (days)) #this is the while statement for the loop
 
    randomNumber = random.randint(1,50)
    salesFigure = (randomNumber*advertisingCost)
    print ("salesFigures is £:" +str(salesFigure)) #this block of code is to work out the sales figure by random number*advertising cost
     
    updatedBalances = (newBalance + salesFigure - dailyexpenses) #this is to display the newbalnce on the users screen
    print ("newbalance is £:" + str(updatedBalances))
    print ("")
     
    sold = salesFigure // priceOfProduct - shopPrice
    if (sold > shopPrice):
        lackOfStock = sold - stock
        costProfit = lackOfStock*priceOfProduct #this is a block of code for If statement showing working out for handling products sold and in stock
        sales = sales - costProfit

 
    #output
 
    if newBalance >=0:
        print ("stock is fine ")
    else:
        print("we need more stock") #this IF statement is to decide if the stock is okay or more stock is needed
 
 
 
costs = (wholeSalePrice,productPurchased) #these are thevariables set for procedures
newBalance = calcNewBalance(newBalance,advertCost,productPurchased)
salesFigure = calculateSalesFigures(0)
dailyExpenses = expenses(0)
updatedBalances = balanceUpdate(newBalance,salesFigure,dailyExpenses)

Commenting is good, but sometimes if too obvious or too long, it is a bit of a problem. Makes it harder to read the code. Some block comments explaining what is going on are usually better in such circumstances.

The input section should be inside the main loop, as the instructions say to repeat step 2. You only do it once. There seems to be some duplication in the questions though, so not sure what is meant to be going on.

The while loop is not executing, because the initial value of days is 0 and the test exits (or bypasses) the loop if days > 0.

Inside the loop, dailyexpenses is referenced before it is defined. Likewise with priceOfProduct, and shopPrice.

You really need to think about going back to paper (yes, paper) with your code and laying out the code structure again. You have a bunch of functions defined, but they are not obviously helping simplify the flow and readability of your code.

I struggled a bit to figure out the calculation of stock holding. Some of the variable naming is very confusing. I appreciate you need to calculate in the simulation the amount of stock sold on the basis of the randomly generated sales values. Just can't get my head around how you are making this calculation as stock quantity and stock value seem to be a bit mixed up.
I am trying to help you, really, even if it doesn't always seem that way
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Issues with while loop, while (var != 0): does not work, but while (var !=''): works danilo 2 2,022 Apr-15-2019, 12:08 AM
Last Post: danilo
  Issues with Inserting Values into an Empty List with a While Loop TommyMer 2 3,732 Sep-12-2018, 12:43 AM
Last Post: TommyMer

Forum Jump:

User Panel Messages

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