Python Forum
How do I calculate a ratio from 2 numbers and return an equivalent list of about 1000
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do I calculate a ratio from 2 numbers and return an equivalent list of about 1000
#5
Only thress changes. First you assign for each iteration the name list_numbers to an empty dict.
You have to initialize this outside of the loop to prevent this. Instead of using a loop, put this code in a function.
This function asks only one time. To repeat this, call this function somewhere else in a while-True loop.

def ratio():
    list_numbers={}
    a = int(input(' Enter 1st number for ratio calculation: '))
    b = int(input(' Enter 2nd number for ratio calculation: '))
     
    y = int(input(' Enter y start range: '))
    n = int(input(' Enter n end range: '))
    for x in range(y,n):
        list_numbers.update({a*x: b*x})
    return list_numbers
The third change is the print function.
Print your results, after you've collected them, otherwise you'll get as first result, then the first two and etc...
This did work with only one result, because the dict was replaced by a new empty dict for each iteration.
But the code has been changed into a function, so the results should be returned and not printed.

Just having one function, which does only one thing, it's easier for later reuse in code and is also easier to change.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Messages In This Thread
RE: How do I calculate a ratio from 2 numbers and return an equivalent list of about 1000 - by DeaD_EyE - Nov-21-2019, 08:03 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  find random numbers that are = to the first 2 number of a list. Frankduc 23 3,295 Apr-05-2023, 07:36 PM
Last Post: Frankduc
  List of random numbers astral_travel 17 2,774 Dec-02-2022, 10:37 PM
Last Post: deanhystad
  Remove numbers from a list menator01 4 1,376 Nov-13-2022, 01:27 AM
Last Post: menator01
  [split] why can't i create a list of numbers (ints) with random.randrange() astral_travel 7 1,571 Oct-23-2022, 11:13 PM
Last Post: Pedroski55
  Divide a number by numbers in a list. Wallen 7 8,109 Feb-12-2022, 01:51 PM
Last Post: deanhystad
  Need to parse a list of boolean columns inside a list and return true values Python84 4 2,144 Jan-09-2022, 02:39 AM
Last Post: Python84
  producing numbers out of a list bouraque7878 10 3,826 Nov-12-2021, 09:13 PM
Last Post: jefsummers
  Found input variables with inconsistent numbers of samples: [1000, 200] jenya56 2 2,922 Sep-15-2021, 12:48 PM
Last Post: jenya56
  How to change odd to even numbers in the list? plumberpy 8 3,802 Aug-08-2021, 11:07 AM
Last Post: plumberpy
  How to invoke a function with return statement in list comprehension? maiya 4 2,913 Jul-17-2021, 04:30 PM
Last Post: maiya

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020