0

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)
3
  • just to be clear, can you give an example of the input? i dont understand if the program will only get the t-shirt size 1 time, or multiple times, one for each player Commented May 6, 2018 at 9:48
  • well the the program i wrote will ask for the size 1 time, but for the answer i need it to ask for 3 times for 3 new players. Commented May 6, 2018 at 10:37
  • well, in the answer i left in here, i gave you the code to do that, hope it helps Commented May 6, 2018 at 10:43

3 Answers 3

2

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
Sign up to request clarification or add additional context in comments.

2 Comments

if psize == "s": total = total + tsmall print("The total cost of equipment is:", total) elif psize == "m": total = total + tmed print("The total cost of equipment is:", total) elif psize == "l": total = total + tlar print("The total cost of equipment is:", total) elif psize == "xl": total = total + txl print("The total cost of equipment is:", total) else: print("enter the correct size").... Thanks for making my code a little shorter. next how will i add diff size price to total. The total should be only one.
Say the input is m, the equipment for the team is calculated before the if-statements and the price for the size is added in the code block of elif psize == "xl". So it now works for one input, but you needed three. I will write some pseudo code in my answer (give me a minute) so you can figure it out.
1

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

6 Comments

thanks for the answer. it works perfectly. Can u please explain to me so that i understand it.
Since you want to know the size of the t-shirt of each player, you will need to ask the same question 3 times, and to avoid reapeating code, you can simply use a while loop, that will loop 3 times, 1 for each player while asking the question " what is the t-shirt size?", after the user inputs the size the program will add the corresponding price to that size to the total cost. Please accept my answer if it was helpful
Thanks a ton. Now to improvise my program i have edited in the last else: print("enter the correct size") so when i enter the incorrect ans for 3 times the program ends with just the total excluding the price of the players. Can we do something so that if we enter the incorrect string it runs again? until we enter the correct size?
yes, the while loop i gave you runs 3 times, 1 for each player, and at the end of the loop it increases the "players" variable so it asks the question again, if the input was not the correct one you can have another if statement in the while loop that only increases the players variable if the input is the correct one.
Hi i want to add the second program to your given solution, i am unable to combine. can you help me out please? #This is the 2nd program sizes = ["S", "M", "L", "XL"] size = input() while size.upper() not in sizes: #.upper is to conver the alphabet to the upper case size = input() # I want to combine both of the programs: If user inputs the any other letter than s,m,l,xl #it will again ask for the size, also if user inputs the letter in lower case it will convert it upper case.
|
0

I think your approach can be like this :

  1. 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): ")
    
  2. 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)

  3. 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
    

6 Comments

OP could consider this but I recommend the use of a while-loop. The use of an extra function is not required in this situation since the prices are already predefined and the if-statements are already set to utilize the price variables.
Thanks for the answer, it says 'price' is not defined :(
@AnandVaswani you need to define the function at the start of the code
@Vallas I have just offered a different approach
@LatikaAgarwal Hi, i want to add a small program in your given solution. If the user inputs wrong size or wrong character, it should ask for the size again. Can you help me out please? This is what i have written for that but i want to combine that with your given solution: sizes = ["S", "M", "L", "XL"] size = input() while size.upper() not in sizes: size = input()
|

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.