Reputation: 3
I just started with learning coding using pycharm. So everything is new for me. I have to write couple of programs which are simple and easy, out of which i have already written and just stuck with this one.
Problem: Design a program that will calculate the total cost of equipment for the 3 new cricket players representing JCU Brisbane Sports Club. The new items are as follows: - Each new player gets knee pads and batting gloves. - The user will be asked the t-shirt size for each new player and based on this the t-shirt price will be added to the total. - In addition, the team also gets 3 new cricket balls and 5 new bats.
Cricket balls cost $20 each, bats $45, knee pads $70 and $130 for a pair of batting gloves. Tshirt sizes are S ($45), M ($55), L ($65) and XL ($75).
The program should return the total cost of the equipment.
What i am unable to do is how to define value for each specific size for each specific player. I am new and stuck. If anyone could help please.
This what i have done so far:
# practise for coding challenge
psize = input("enter the size of the player1(s/m/l/xl): ")
#psize2 = input("enter the size of the player:")
cricBall = 20
cricBat = 45
kPad = 70
batGlove = 130
tsmall = 45
tmed = 55
tlar = 65
txl = 75
if psize == "s":
total = (3 * kPad) + (3 * batGlove) + 45 + (3 * cricBall) + (5 * cricBat)
print("The total cost of equipment is:", total)
if psize == "m":
total = (3 * kPad) + (3 * batGlove) + 55 + (3 * cricBall) + (5 * cricBat)
print("The total cost of equipment is:", total)
if psize == "l":
total = (3 * kPad) + (3 * batGlove) + 65 + (3 * cricBall) + (5 * cricBat)
print("The total cost of equipment is:", total)
if psize == "xl":
total = (3 * kPad) + (3 * batGlove) + 75 + (3 * cricBall) + (5 * cricBat)
print("The total cost of equipment is:", total)
Upvotes: 0
Views: 270
Reputation: 969
I think your approach can be like this :
Ask for 3 inputs of t-shirt size of each player
psize_player1 = input("enter the size of the player1(s/m/l/xl): ")
psize_player2 = input("enter the size of the player1(s/m/l/xl): ")
psize_player3 = input("enter the size of the player1(s/m/l/xl): ")
Now in cost of equipment only variable quantity is price of T-shirt as per size.
cost of equipment = (3 * kPad) + (3 * batGlove) + (3 * cricBall) + (5
* cricBat) + price(psize_player1) + price(psize_player2) + price(psize_player3)
where price(psize_player1) is a function which returns price as per t-shirt size (see next step)
Define a function which checks size and return price.
def price(size):
if size == small:
return tsmall
elif size == med:
return tmed
elif size == large:
return tlar
elif size == extralarge:
return txl
Upvotes: 0
Reputation: 503
You already have a good start. I would prefer not to give any code since you could learn more by figuring this out yourself with a little help. However I can add it in later if you really can't figure it out.
First of all instead of using different if-statements
, you can work with if, elif and perhaps an else-statement
. Like the fake code below:
if statement:
do this
elif statement:
do this
elif statement:
do this
else:
do this
As for your question: You already have the prices of each size predefined and you print a variable based on the input. All you would have to do is add each size to total
in the right statement. For example at the code below we add the price:
if psize == "s":
total = (3 * kPad) + (3 * batGlove) + 45 + (3 * cricBall) + (5 * cricBat) + tsmall
print("The total cost of equipment is:", total)
Now work in a similar manner on the other statements.
One more thing though: since you're doing the same operation in every if-statement
, you could do this before those statements. Like this:
total = (3 * kPad) + (3 * batGlove) + 45 + (3 * cricBall) + (5 * cricBat)
if psize == "s":
total = total + tsmall
print("The total cost of equipment is:", total)
And once again: do the same for the other statements.
Solution to the comment below:
#calculate base price
total = 3 * (kPad + batGlove + cricBall) + 45 + 5*cricBat
#Loop three times, ask for an input and add the price per input to total
count = 0;
while count < 3:
#ask for input here
#add size-based price to total (with the if-statements)
count += 1
#exit loop and print the result
Upvotes: 2
Reputation: 449
If each of the 3 players will have a different t-shirt size, you can simple do this:
cricBall = 20
cricBat = 45
kPad = 70
batGlove = 130
tsmall = 45
tmed = 55
tlar = 65
txl = 75
total = (3 * kPad) + (3 * batGlove) + (3 * cricBall) + (5 * cricBat)
players = 0
while(players < 3):
psize = input("enter the size of the player1(s/m/l/xl): ")
if psize == "s":
total = total + tsmall
elif psize == "m":
total = total + tmed
elif psize == "l":
total = total + tlar
else:
total = total + txl
players += 1
print("The total cost of equipment is:", total)
like it was said in the other answer, simply calculate the total cost of the players equipment and for each player add the cost of the t-shirt to the total cost, after that print the total cost
Upvotes: 1