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()
Advertisement
Answer
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.
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 (https://www.freecodecamp.org/news/python-for-loop-example-and-tutorial/). After you build a loop. Add another piece (like appending items to a list like I did below), and so on. Continue with some patience, and you will be rewarded with better python skills.
I’m still fairly new to the stack overflow community, but here are some tips for getting good responses to questions:
make your question as specific as possible, i.e., ‘what’s the proper syntax for defining a function in python’ is better than ‘I need a function and some loops and other things to make this script better’
then before you post it, search to see if someone else already asked/answered your question
if there is no answer, ask it here and provide the code involved (which you did nicely)
Best of luck!
Note: the calculations you did (which I copied exactly) didn’t make much sense to me, but I also don’t know exactly what this is for. Anyways, feel free to comment if you have any questions!
#create wt_convert function which has two required parameters: list of weights and total cost def wt_convert(list_of_weights, total_cost): unit = "pounds" pounds = 0.00220462 #create empty list to hold converted weights converted_weights = [] #loop through the list of weights, convert them, add them to 'converted_weights' list for wt in list_of_weights: conversion = float(wt * pounds) converted_weights.append(conversion) #calculate total weight by using sum function and passing list of 'converted_weights' totalweight = sum(converted_weights) #loop through 'converted_weights', calculate price, print result for each i = 1 n = 0 for wt in converted_weights: price = (wt / totalweight) * (total_cost * 3) print('Specimen {} is {} {} and is worth $ {}'.format( i, converted_weights[n], unit, price )) i += 1 n += 1 #now function (above) is ready, prepare the needed parameters to use it specimen_weights = [] #used 'while loop' to enable user to make as many inputs as desired keep_going = "y" i = 1 while keep_going == "y": wt = int(input("What is the weight of specimen {}: ".format(i))) specimen_weights.append(wt) i += 1 keep_going = input("To add another specimen, input 'y'. Otherwise press enter: ") #ask user for total cost totalcost = float(input("What is the total price of the lot?: ")) #now call function and pass list of specimen weights and totalcost wt_convert(specimen_weights, totalcost)