-1
def thrust_flow_function(M,r):
  G = (1+r*math.pow(M,2))/(math.pow((1+(r-1)/2*math.pow(M,2)),(r/(r-1))))
  dG = r*G*M*((2/(1+r*math.pow(M,2)))-(1/1+(r-1)/2*math.pow(M,2)))
  return [round(G,5),round(dG,5)]
M = float(input())
r = float(input())
print((thrust_flow_function(float(M),float(r))))

In the current code, I'm returning both the values as list. However, when I try to return both the values as separate

return G return dG

it only gives me the value of G

Kartik
  • 9
  • 2
  • 1
    Square brackets have special meaning in Python, where they denote lists. The `dG` formula should likely use parentheses instead. – nanofarad Sep 21 '20 at 22:07
  • Thanks a lot, man. Really appreciate it. Wasted like 2 hrs on this. I thought the error is with the power function. – Kartik Sep 21 '20 at 22:09
  • There is no need to call `float` so many times. `M` and `r` are already floats. Also, `M*M` is much faster than `math.pow(M,2)`. – DYZ Sep 21 '20 at 22:15
  • Hey, I got one more doubt... In the current code I'm returning both the values as list (as shown)... `return [round(G,5),round(dG,5)]` However, when I try to return both the values as separate(as shown...`return G` `return dG`), it only gives me the value of `G` – Kartik Sep 21 '20 at 22:23
  • @Kartik when you hit the first return, the function ends. Returning them as a list is one option. Another is a tuple: `return round(g, 5), round(dG, 5)` – nanofarad Sep 21 '20 at 22:28
  • @nanofarad so if I want the values as separate entities, will I have to run them as separate functions – Kartik Sep 21 '20 at 22:31
  • @Kartik Return the tuple and unpack it at the receiving end if you need. [This](https://stackoverflow.com/a/36632980/1424875) shows a simple example. – nanofarad Sep 21 '20 at 23:03

2 Answers2

1

Square brackets ([]) are used to denote lists. You can't use them as general-purpose parentheses like you sometimes do in math. Replace them with good old parentheses in the calculation of dG and you should be OK:

dG = r*G*M*((2/(1+r*math.pow(M,2)))-(1/1+(r-1)/2*math.pow(M,2)))
# Here-----^---------------------------------------------------^
Mureinik
  • 297,002
  • 52
  • 306
  • 350
0

r*G*M is producing a float, and you're multiplying that by the following anonymous list (created due to the use of square brackets, []). You can't do that. Did you mean to use additional parentheses, (), instead of []?

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
  • Thanks, I replaced it with round brackets and it worked. – Kartik Sep 21 '20 at 22:15
  • Hey, I got one more doubt... In the current code I'm returning both the values as list (as shown)... 'return [round(G,5),round(dG,5)]' However, when I try to return both the values as separate(as shown), it only gives me the value of G 'return G' 'return dG' – Kartik Sep 21 '20 at 22:15
  • @Kartik: The function is done when you execute `return`, you can't `return` multiple times (`yield` is kinda like doing that, but it gets used in a different way). Typically, you'd omit the square brackets and just do `return round(G,5),round(dG,5)` [to return multiple items](https://stackoverflow.com/q/354883/364696), as `tuple`s are more efficient for the purpose anyway (you don't need `list` features). – ShadowRanger Sep 21 '20 at 22:48
  • ` dN = N*((1/M)+((r-1)/2)(M/(1+(r-1)/2*pow(M,2)))-(2*r*M/(1+r*pow(M,2)))) ` This line of code is again giving me that error...**'float' object is not callable**. Now, whats the error. Sorry for asking all the silly questions but I'm new to python. – Kartik Sep 21 '20 at 22:55
  • @Kartik: Python is not math; you can't just put terms next to each other and get implicit multiplication; you need a `*` where you have `)(` directly adjacent to one another if you want to multiply. Given your (and everyone's) issues with reading complex formulae, I'd suggest breaking them down into smaller bits and storing off partial computations, then operating on those partials (which would also reduce the number of parentheses you need for grouping). – ShadowRanger Sep 21 '20 at 22:56
  • Alright. Thanks for your guidance. – Kartik Sep 21 '20 at 23:01