一个float变量,被神秘地当作一个字符串

时间:2019-02-13 21:20:33

标签: python shell

这是我代码的主要内容

 #!/usr/bin/env python
    import time
    import sys
    import numpy
    import random
    from random import randint  
    from configurable_trace_gen import ssatrace, random_trace

    def main():
        K = random.randint(150, 200)
        if (len(sys.argv) >= 3):
            probability = [float(x) for x in sys.argv[1:3]]
            ratio=sys.argv[-1]
            trace = random_trace(K, probability, ratio)
            run=ssatrace(trace)
            print( K, probability, ratio)
            print(run)
        else:
            print('Params Error')
    if __name__ == "__main__":
        main()

,我有这个shell脚本可以多次执行 main.py (上面的代码),同时更改输入中的参数: $ short $长 $比率

#!/bin/bash
step=0.05
short=0
long=1
for firstloop in {1..20}; do
    ratio=0
    for secondloop in {1..20} ; do
        echo $short $long $ratio >> file.txt
        time python3 main.py "$short" "$long" "$ratio">> output.txt
        ratio=$(echo "scale=10; $ratio + $step" | bc)
    done
    short=$(echo "scale=10; $short + $step" | bc)
    long=$(echo "scale=10; $long - $step" | bc)
done

执行时,出于某种原因,变量 ratio 被认为是一个字符串,我不理解为什么! ratio 应该采用命令行中3个参数的最后一个值,而 probability 是应该包含 $ short $ long

这是我得到的输出的片段:

TypeError: unsupported operand type(s) for -: 'int' and 'str'

real    0m0.511s
user    0m0.416s
sys 0m0.076s
Traceback (most recent call last):
  File "main.py", line 45, in <module>
    main()
  File "main.py", line 20, in main
    trace = random_trace(K, probability, ratio)
  File "/Users/ouafaelachhab/Desktop/SemanticLocality/src/configurable_trace_gen.py", line 34, in random_trace
    ratioList=[ratio, 1-ratio]
TypeError: unsupported operand type(s) for -: 'int' and 'str'

0 个答案:

没有答案