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
#1
I was online at this amazing site for equivalent ratios. I would love to mimic or do the same as an output in python, but its a bit over my skills.

Can you help me put it in this format, here is what the first 3 pairs would look like in the output in python, if you can help me please?

1 : 5618 2 : 11236 3 : 16854

Here is the site that does what I want:

https://goodcalculators.com/ratio-calculator/

Here is my code so far but I hope someone can help me so it prints out in a nice fancy format or just maybe a cool list of numbers in equivalent ratio format:

It is throwing an error and I don't know why, and this is the error:
ValueError: invalid literal for int() with base 10: ' Enter 1st number for ratio calculation: '

while True:
        a = input(int(' Enter 1st number for ratio calculation: '))
        b = input(int(' Enter 2nd number for ratio calculation: '))
        r = ( a/b and b/b)
        print(r)
Reply
#2
Replace input(int(...)) with int(input(...)). Currently you are trying to convert a sentence into an integer and that puzzles the python interpreter.
Reply
#3
Hi! Check lines 2 and 3, you mixed up input and int, it should be vice versa: int(input(...)).
Good luck!
Reply
#4
Thanks guys however I'm trying to create a clean print though. This print job repeats, and with a little help at stackexcange along with my modification we got it to work. However, checkout the print feature. Can anyone help me with a single quality print here. Here is the code I have. Run it and see what it prints.

Also:

While I print using this python37 code it repeats till the end and its slow. I really need it to print super fast. Also I would like to divide the first number it finds before the colon and output the number that it is associated with after the colon.

For instance the program finds this ratio pair ( 8978:1445580) in the set after going through an amount of numbers and I decided to divide it like this 8978/8978 then the output should print (1445580). Or maybe it just matches 8978 and prints (1445580) as the output.

Is this possible? It does seem programmable, but my skills are a little weak in this area and I could use some help.

Thanks very much for any help! :)

Here is the code I'm working with:

while True:
    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})
        print(list_numbers)
Reply
#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
#6
@Anyone

How do I print a specific denominator of a ratio?

The code outputs ratios in this format of a dictionary like this:



{1: 8978, 2: 17956, 3: 26934, 4: 35912, 5: 44890, 6: 53868, 7: 62846, 8: 71824, 9: 80802}
I would like to print the denominator of a ratio by using a specific input.

For instance, I want to use some unknown input for the program to find this ratio pair ( 5: 44890) and while going through a series of numbers in the dictionary during the codes processing; I would like it to print (44890) at the conclusion of its processing.

while True:
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: '))

list_numbers = {(a*x): (b*x) for x in range(y,n)}
print(list_numbers)

while True:
          list_numbers={}
          a = int(input(' Enter 1st number for ratio calculation: '))
          b = int(input(' Enter 2nd number for ratio calculation: '))
          d = int(input(' Enter a number to divide ratio before colon: '))
    
          y = int(input(' Enter y start range: '))
          n = int(input(' Enter n end range: '))
    
          list_numbers = {((a*x)%(d)): b*x for x in range(y,n)}
          print(list_numbers)
Reply
#7
(Nov-21-2019, 09:02 PM)Pleiades Wrote: {1: 8978, 2: 17956, 3: 26934, 4: 35912, 5: 44890, 6: 53868, 7: 62846, 8: 71824, 9: 80802}
For instance, I want to use some unknown input for the program to find this ratio pair ( 5: 44890) and while going through a series of numbers in the dictionary during the codes processing; I would like it to print (44890) at the conclusion of its processing.

I don't really understand what you're trying to do. How does the input relate to what you're trying to find? Imagine you didn't have a computer and had this collection of values written down on paper. How would you find the one you want? With quite small problems like this, you should be trying to figure out the steps in getting to the solution. Code will naturally follow from those (assuming you know the right code elements to implement your solution, but you need to start at finding the solution first).
Reply
#8
By splitting the range between the two numbers into equal intervals and computing the corresponding values, you may compute a ratio between two numbers and generate a list of around 1000 equivalent values.

The two integers in this example that you wish to compute the ratio for are numbers 1 and 2. These two numbers are passed to the calculate_ratio_list function together with the required number of entries (num_values) to be included in the result list. After calculating the ratio, it applies the ratio to the first integer in a geometric progression to get a list of roughly 1000 values.
buran write Jan-05-2024, 07:06 AM:
clickbait link removed
Reply
#9
(Nov-21-2019, 09:02 PM)Pleiades Wrote: @Anyone

How do I print a specific denominator of a ratio?

The code outputs ratios in this format of a dictionary like this:


That ratio calculator/generator has 2 features. One is it displays the multiples of the ratio you chose and two, it solves the equation for the equality. These two are separate things and are not done at once. It looks like you are either trying to do both at the same time, or you are trying to solve the equality problem in a roundabout way.
What is it that you are actually trying to do?
Reply


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,015 Apr-05-2023, 07:36 PM
Last Post: Frankduc
  List of random numbers astral_travel 17 2,533 Dec-02-2022, 10:37 PM
Last Post: deanhystad
  Remove numbers from a list menator01 4 1,251 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,430 Oct-23-2022, 11:13 PM
Last Post: Pedroski55
  Divide a number by numbers in a list. Wallen 7 7,926 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,037 Jan-09-2022, 02:39 AM
Last Post: Python84
  producing numbers out of a list bouraque7878 10 3,625 Nov-12-2021, 09:13 PM
Last Post: jefsummers
  Found input variables with inconsistent numbers of samples: [1000, 200] jenya56 2 2,807 Sep-15-2021, 12:48 PM
Last Post: jenya56
  How to change odd to even numbers in the list? plumberpy 8 3,622 Aug-08-2021, 11:07 AM
Last Post: plumberpy
  How to invoke a function with return statement in list comprehension? maiya 4 2,753 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