【问题标题】:How do I calculate a ratio from 2 numbers and return an equivalent list of about 1000 numbers?如何计算 2 个数字的比率并返回大约 1000 个数字的等效列表?
【发布时间】:2019-11-20 04:41:08
【问题描述】:

我在这个惊人的网站上在线获得了同等比率。我很想模仿python中的输出或做同样的事情,但这有点超出我的技能。

你能帮我把它改成这种格式吗,这是python输出中前3对的样子,如果你能帮助我吗?我正在使用 python37

1:5618 2:11236 3:16854

这是我想要的网站: https://goodcalculators.com/ratio-calculator/

到目前为止,这是我的代码,但我希望有人可以帮助我,以便它以漂亮的花哨格式打印出来,或者只是以等效比率格式打印出一个很酷的数字列表:

它抛出一个错误,我不知道为什么,这是错误:

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)

【问题讨论】:

    标签: python calculation


    【解决方案1】:

    你的整数应该是第一位的:

       while True:
            a = int(input(' Enter 1st number for ratio calculation: '))
            b = int(input(' Enter 2nd number for ratio calculation: '))
            r = ( a/b and b/b)
            print(r)
    

    对于计算器,您可以这样做:

    list_numbers={}
    ratio1 = 1
    ratio2 = 3698
    for x in range(1,100):
       list_numbers.update({ratio1*x: ratio2*x})
    

    【讨论】:

    • oppressionslayer 你最近帮了我很多,所以如何创建一个包含 1000 个数字的列表,比如这个小例子,1:5618 2:11236 3:16854
    • 您要列表还是集合,[ [1,5618] ], [ 2, 11236 ], [3, 16854]] 或设置 { 1:5618, 2:11236}, {3 ,16854} 这些只是随机数吗?我会使用 for 循环
    • 第一个输入是 [1] 第二个是 [3698]
    • 是的,请从这些第一个输入开始,例如 goodcalculators 的比率
    • 我想我刚刚更新了它,你可以修改它以使用你的输入 a 和 b
    【解决方案2】:

    根据您系统上的 python 版本,有两个功能。 Python3 有 input(),python2 有 raw_input(),只返回字符串对象。因此您需要将其显式转换为整数。对于 python3,您可以执行以下操作,对于 python2,您可以在 raw_input() 之前使用 eval() 函数,以便您可以评估表达式。

    while True:
        a = int(input(' Enter 1st number for ratio calculation: '))
        b = int(input(' Enter 2nd number for ratio calculation: '))
        r = ( a/b and b/b)
        print(r)
    

    Python2:

    x=1
    print eval('x+1') ==> 2
    

    【讨论】:

      猜你喜欢
      • 2013-09-04
      • 1970-01-01
      • 2021-05-14
      • 1970-01-01
      • 1970-01-01
      • 2017-04-18
      • 1970-01-01
      • 2020-06-21
      • 1970-01-01
      相关资源
      最近更新 更多