0

When I run the bash program that calls the python script below I get this error: line 12, in roundup return int(int(math.ceil(x / 1000)) * (10**3)) TypeError: unsupported operand type(s) for /: 'str' and 'int' When the python script is run by itself there are no issues or errors. I checked to make sure that the bash variable was not a float and it is not on both programs. I am not sure how to fix this. I tried int(int(math.ceil(x / 1000)) * (10****3)) and int(math.ceil(x / 1000)) * (10**3)

import math
import os

pdiv=os.environ["div"]
print(pdiv)
#pdiv=931573 in both the bash srcipt that is calling this python script and the python script
divi=str(pdiv)
divide=len(divi)


if divide >= 4 and divide < 7:
    def roundup(x):
        return int(int(math.ceil(x / 1000)) * (10**3))
    z=roundup(pdiv)
    array = [int(x) for x in str(z)]
    if z == 4: 
       byte=str(array[0]) +'k'
        
        
    elif z == 5:
        byte=str(array[0]) + str(array[1]) + 'k'
        
    elif divide == 6:
        byte=str(array[0]) + str(array[1]) + str(array[2]) + 'k'
        

elif divide >= 7 and divide < 10:
    def roundup(x):
        return int(int(math.ceil(x / 1000000)) * (10**6))
    z=roundup(pdiv)
    array = [int(x) for x in str(z)]
    if divide == 7:
        byte=str(array[0]) +'m'
        
    elif divide == 8:
        #byte=print(array[0], end="");print(array[1], end="");print('m')
        byte=str(array[0]) + str(array[1]) + 'm'
        
    elif divide == 9:
        byte=str(array[0]) + str(array[1]) + str(array[2]) + 'm'
        
    
file = open("Bytes.txt", "w")
file.write(byte)
file.close

2
  • 2
    The values from os.environ[...] are always strings. Convert pdiv to int or float. Commented Aug 4, 2021 at 16:23
  • Don't define functions inside if blocks, define them globally. If you need different denominators and exponents, make them function parameters. Commented Aug 4, 2021 at 16:26

1 Answer 1

1

The error is from the expression: x / 1000. In your script, x is a string, which you get from an environment variable. When you attempt to divide a string by an int, you get that error. To fix, you need to convert the string into a number:

def roundup(x):
    x = int(x)
    return int(int(math.ceil(x / 1000)) * (10**3))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.