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.
My code examples are always for Python >=3.6.0
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


Possibly Related Threads…
Thread Author Replies Views Last Post
  convert numbers into list lokesh 1 272 Jun-03-2021, 06:37 AM
Last Post: menator01
  calculate daily return in percent in forex as to some strategy? alen 1 366 Mar-12-2021, 10:03 AM
Last Post: buran
  matching with SequenceMatcher ratio two dataframe zoropython 2 443 Feb-13-2021, 01:45 PM
Last Post: zoropython
  Calculate column with cumulative return tgottsc1 1 397 Jan-25-2021, 12:52 PM
Last Post: buran
  adding numbers in a list Nickd12 2 435 Jan-15-2021, 12:46 PM
Last Post: Serafim
  Regular expression: return string, not list Pavel_47 3 428 Jan-14-2021, 11:49 AM
Last Post: Pavel_47
  Stumped by my own code (ratio & epoch-time calculation). MvGulik 2 476 Dec-30-2020, 12:04 AM
Last Post: MvGulik
  Print max numbers in a list jimmoriarty 1 460 Sep-25-2020, 07:29 AM
Last Post: DPaul
  importing a list of numbers into python script barrypyth 8 1,086 Aug-22-2020, 09:10 PM
Last Post: barrypyth
  Summing a list of numbers Oldman45 6 768 Jul-12-2020, 05:30 PM
Last Post: Oldman45

Forum Jump:

User Panel Messages

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