Skip to content
Advertisement

Trying to change my code to include loops and listed variables to make more streamline and expandable code

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.

JavaScript

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.

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

  2. 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!

JavaScript

example output: enter image description here

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement