Help for Linear Dilation calculations in Python

Asked

Viewed 85 times

1

Hello. This is my first post on Stackoverflow so a thousand pardons if I ever make a mistake.

Guys, can you help me? I’m trying to develop a code in Python that can calculate the Linear Dilation of some material informed via function. But I have a problem with setting alpha in the equation. In physics, an example of alpha for linear dilation calculations is (10 * 1.2 -5)°C -1. But if I put this calculation into a python variable, when trying to print, it displays this as a calculation and not as a value.

An example:

Linear expansion of an iron plate whose (delta)T (final temperature minus initial temperature) will be 30m, the plate size will be 100m and the alpha will be (10*1.2 -5)°C -1

Delta L = 100 x (10*1,2 -5) x 30 Delta L = 3600 -5m °C -1 (I’m not continuing to avoid some tithe)

This is the result of equation 3600 -5 m °C -1

If I wanted to play this same calculation in python and make the printed result the same, as I should do?

  • Welcome, Show us how you tried to do in Python...

  • i didn’t even try because I already knew that the interpreter would consider (10*1,2 -5)°C -1 as a calculation, I want to know how I can do this code

  • Hummm which Python version is using ?

  • This is confusing. Both * and x in its code represent multiplication? 10*1,2^-5 can be written as 1.2 * (10 ** -5), that helps?

1 answer

0

I don’t know which Python version you are using, but for the Python3.X version I would simply do so:

Placa=100
Temperatura=30
L=Placa*(10*1.2) * Temperatura / 10    
print(int(L),'^-5 m °C ^-1',sep='')

Upshot:

Python 3.6.1 (default, Dec 2018, 13:05:11)
[GCC 4.8.2] on linux
3600^-5 m °C ^-1

Another way as per my comment:

alpha='10*1.2^-5'
Placa=100
Temperatura=30
[calc, string ]=alpha.split('^')
L=Placa*(eval(calc)) * Temperatura / 10    
print(int(L),'^',string,' m °C ^-1',sep='')

Upshot:

Python 3.6.1 (default, Dec 2018, 13:05:11)
[GCC 4.8.2] on linux
3600^-5 m °C ^-1
  • face the problem is there. The alpha of the equations is different from material to material. i wanted to do a function where I passed the values and it displayed the calculation but python (I use 3.6) does not understand 10x1,2 -5 as an information but rather as a calculation

  • If the code is practically the same, create two more variables an alpha=10*1.2 call and another string type variable receiving " -5" then just concatenate like I did in print... if you want to be smarter still could put everything in one variable and then give a split ... see the answer again

  • Thank you for helping me. I was able to do the code based on what ederwander did. I put the code in a function and put an if to prevent '.' replacement errors by ','.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.