Home > Blockchain >  Trying to change my code to include loops and listed variables to make more streamline and expandabl
Trying to change my code to include loops and listed variables to make more streamline and expandabl

Time:09-15

I made a prior discussion where I was unable to properly explain myself. This is my most up to date code that does what I want with 3 items. I would like to turn this into a function using a list so that it can ask how many items you want to use and then run the code that I have so it is not manually inputted 3 separate times but is instead expandable to however many items are needed.



unit = "pounds"
pounds = 0.00220462

weightA = float(input("What is the weight of Specimen A? "))

converted_weightA = float(weightA * pounds)
formatted_floatA = "{:.2f}".format(converted_weightA)
#print('Specimen A is '   formatted_floatA   ' '   unit)


weightB = float(input("What is the weight of Specimen B? "))

converted_weightB = float(weightB * pounds)
formatted_floatB = "{:.2f}".format(converted_weightB)
#print('Specimen B is '   formatted_floatB   ' '   unit)


weightC = float(input("What is the weight of Specimen C? "))

converted_weightC = float(weightC * pounds)
formatted_floatC = "{:.2f}".format(converted_weightC)
#print('Specimen C is '   formatted_floatC   ' '   unit)


totalcost = float(input("What is the total price of the lot?:    "))
totalweight = converted_weightA   converted_weightB   converted_weightC

IndividualPriceA = (converted_weightA / totalweight) * (totalcost * 3)
IndividualPriceB = (converted_weightB / totalweight) * (totalcost * 3)
IndividualPriceC = (converted_weightC / totalweight) * (totalcost * 3)

print('Specimen A is '   formatted_floatA   ' '   unit   ' and is worth $',IndividualPriceA)
print('Specimen B is '   formatted_floatB   ' '   unit   ' and is worth $', IndividualPriceB)
print('Specimen C is '   formatted_floatC   ' '   unit   ' and is worth $', IndividualPriceC)


input()

CodePudding user response:

Strock, glad your here. I created a solution for you (below) but I wanted to offer some key advice you would benefit from first.

Your question isn't a hard one, but it's almost asking for someone to turn your code into a usable, repeatable script (your asking for loops, a function, etc.). In other words, it comes across as more of a 'will someone do this for me' vice 'How do I do [A] with Python'.

I'm not sure what your profession or intention is regarding python, but if you are seeking to gain more competence with the programming language, the following tips are much more important for you than the solution below.

  1. Break down your script into small parts, i.e., you know you need loops, so start by creating a very simple loop, understanding it's syntax (enter image description here

  • Related