Reputation: 105
Basically I want to input 10 q values and their 10 respective cr values and then have it print out 10 k4 values.
I get the error:unsupported operand type(s) for /: 'list' and 'float'.
I know this must fundamentally be me not understanding how lists work, but I've been reading up on them via googling and automate the boring stuff and don't really understand.
This must be because the input is a list and the equation is asking for floats?
import math
qmax = 11.3
q = input("Enter q values separated by a comma. ").split(',')
cr = input("Enter cr values separated by a comma.").split(',')
for i in q and cr:
k4 = cr * ((-math.log(1 - ((q / qmax) ** (1 / 3)))) +
(0.5 * math.log(1 + ((q / qmax) ** (1 / 3)) + ((q / qmax) ** (2 / 3)))) +
(math.sqrt(3) * math.atan(((math.sqrt(3)) * ((q / qmax) ** (1 / 3))) / (2 + ((q / qmax) ** (1 / 3))))))
print(k4)
Upvotes: 0
Views: 62
Reputation: 1223
Your code here has issues because q is a list
k4 = cr * ((-math.log(1 - ((q / qmax) ** (1 / 3)))) +
(0.5 * math.log(1 + ((q / qmax) ** (1 / 3)) + ((q / qmax) ** (2 / 3)))) +
(math.sqrt(3) * math.atan(((math.sqrt(3)) * ((q / qmax) ** (1 / 3))) / (2 + ((q / qmax) ** (1 / 3))))))
e.g.
input: 3,4,5,6 then
q = [3,4,5,6]
q/qmax = [3,4,5,6]/11.3
also your loop is wrong since it is moving down q but not moving down cr.
your i refers to cr. do this instead:
for i in range(len(q)):
# reference the q with q[i] and the corresponding cr with cr[i]
Upvotes: 0
Reputation: 8318
First you want to iterate on both the input arrays, one way of doing so is using zip
.
Second, you want to perform numeric operation-> you need to transform the element to float
.
Both changes can be seen in the following:
import math
qmax = 11.3
q = input("Enter q values separated by a comma. ").split(',')
cr = input("Enter cr values separated by a comma.").split(',')
for i_q, i_cr in zip(q,cr):
i_q = float(i_q)
i_cr = float(i_cr)
k4 = i_cr * ((-math.log(1 - ((i_q / qmax) ** (1 / 3)))) +
(0.5 * math.log(1 + ((i_q / qmax) ** (1 / 3)) + ((i_q / qmax) ** (2 / 3)))) +
(math.sqrt(3) * math.atan(((math.sqrt(3)) * ((i_q / qmax) ** (1 / 3))) / (2 + ((i_q / qmax) ** (1 / 3))))))
print(k4)
Which gives the following output:
Enter q values separated by a comma. 1,2,3
Enter cr values separated by a comma.1,2,3
1.3680605611830536
3.5350475424910037
6.2401913565748846
Upvotes: 2