I want to append float to list, but I got an error like this:
<ipython-input-47-08d9c3f8f180> in maxEs()
12 Es = lists[0]*0.3862 + lists[1]*0.3091 + lists[2]*0.4884
13 aaa = []
---> 14 Es.append(aaa)
15
AttributeError: 'float' object has no attribute 'append'
I guess I can't append float to list. Can I add floats to list another way?
This is my code:
import math
def maxEs():
for a in range(1, 101):
for b in range(1,101):
for c in range(1,101):
if a+b+c == 100 :
lists = []
lists.append(a*0.01)
lists.append(b*0.01)
lists.append(c*0.01)
Es = lists[0]*0.3862 + lists[1]*0.3091 + lists[2]*0.4884
aaa = []
Es.append(aaa)
I don't know what you want, but you are trying to append a list to a float not the other way round.
Should be
aaa.append(Es)
The other answer already explained the main problem with your code, but there is more:
as already said, it has to be aaa.append(Es) (you did it right for the other list)
speaking of the other list: you don't need it at all; just use the values directly in the formula
aaa is re-initialized and overwritten in each iteration of the loop; you should probably move it to the top
you do not need the inner loop to find c; once you know a and b, you can calculate c so that it satisfies the condition
you can also restrict the loop for b, so the result does not exceed 100
finally, you should probably return some result (the max of aaa maybe?)
We do not know what exactly the code is trying to achieve, but maybe try this:
def maxEs():
aaa = []
for a in range(1, 98 + 1):
for b in range(1, 99-a + 1):
c = 100 - a - b
Es = 0.01 * (a * 0.3862 + b * 0.3091 + c * 0.4884)
aaa.append(Es)
return max(aaa)
Related
list1 = [140,232,857,273,405,374,1234,394,1803]
u = 0
b = 4
for i in list1[u:b]
u+= 4
u+= 4
print(max(i))
Now I wanna take the max value from that list but only from list1[0:4] and continue with that.
Now I want to do something like it on this code:
for im in images:
ww, hh = zip(*(im.size for im in images))
www, hhh = im.size
max_h = max(hh)
y_test = []
try:
new_im.paste(im, (x_offset,y))
with open('x.txt', 'a') as file:
file.write(str(x_offset) + "\n")
with open('y.txt', 'a') as file:
file.write(str(y) + "\n")
with open('w.txt', 'a') as file:
file.write(str(www) + "\n")
with open('h.txt', 'a') as file:
file.write(str(hhh) + "\n")
x_offset += im.size[0]
if x_offset > int(q_w) - www:
print(max(hh))
x_offset =0
y += max(hhh)
if hh < y:
y += hhh
if hh > y:
y -= hhh
else:
y += max_h
except:
continue
if x_offset > int(q_w) - www then I want to take the max value of hhh until here.
How can I do that?
Please understand that we apply max( ... ) to a sequence,
rather than to a single scalar value,
e.g. max([6, 7, 8]) rather than max(4).
That first example was unclear.
I think your intent is to run a window of size k=4 over the list
and display local maxima.
A more natural way to express that, without incrementing u inside the loop,
would be:
for i in range(len(list1) - k):
window = list1[i : i + k]
print(i, max(window))
A very similar approach would apply to your second example.
Phrase the for loop in this way, and slice off k elements:
for i, im in enumerate(images):
if i + k < len(images):
window = images[i : i + k]
...
After that you're on your own,
do something useful with window,
as your question was unclear on details of what you want.
You wrote this line within the loop:
ww, hh = zip(*(im.size for im in images))
It computes the same thing each time, so to make things quicker
it belongs outside the loop.
Additionally, it trashes the im iteration variable,
so for the rest of the loop im is a constant value,
it is always the last element of images.
This seems Bad, it's probably not what you wanted.
Similarly, this is a constant
which could be hoisted outside the loop:
max_h = max(hh)
Also, your except: continue is correct,
but except: pass would be the more usual idiom,
expressing the intent to simply ignore the exception.
No statements follow it in the code you posted,
so both would work out the same.
Understand that continue would skip to top-of-loop,
skipping the following statements if there were any.
The task is:
User enters a number, you take 1 number from the left, one from the right and sum it. Then you take the rest of this number and sum every digit in it. then you get two answers. You have to sort them from biggest to lowest and make them into a one solid number. I solved it, but i don't like how it looks like. i mean the task is pretty simple but my code looks like trash. Maybe i should use some more built-in functions and libraries. If so, could you please advise me some? Thank you
a = int(input())
b = [int(i) for i in str(a)]
closesum = 0
d = []
e = ""
farsum = b[0] + b[-1]
print(farsum)
b.pop(0)
b.pop(-1)
print(b)
for i in b:
closesum += i
print(closesum)
d.append(int(closesum))
d.append(int(farsum))
print(d)
for i in sorted(d, reverse = True):
e += str(i)
print(int(e))
input()
You can use reduce
from functools import reduce
a = [0,1,2,3,4,5,6,7,8,9]
print(reduce(lambda x, y: x + y, a))
# 45
and you can just pass in a shortened list instead of poping elements: b[1:-1]
The first two lines:
str_input = input() # input will always read strings
num_list = [int(i) for i in str_input]
the for loop at the end is useless and there is no need to sort only 2 elements. You can just use a simple if..else condition to print what you want.
You don't need a loop to sum a slice of a list. You can also use join to concatenate a list of strings without looping. This implementation converts to string before sorting (the result would be the same). You could convert to string after sorting using map(str,...)
farsum = b[0] + b[-1]
closesum = sum(b[1:-2])
"".join(sorted((str(farsum),str(closesum)),reverse=True))
I know that this has been asked already, but I'm having trouble with how I should implement this so I'm asking here.
My python code so far is:
def calculate():
p = 10000 # dollars
n = 12 # months
r = 8 # interest %
t = float(raw_input("Type the number of year that the money will be compounded for:"))
b = p * r
a = b ** t
print(a)
calculate()
Math formula
Like John Coleman mentioned in the comments you didn't implement the formula at all.
In your code you just multiplied p with r and b to the power of t.
The right formula looks like this: p*(1+(r/n))**(n*t).
I would recommend you to read an article related to python basic operators you can find one on Python Course.
def calculate():
p = 10000
n = 12
r = .08
t = float(input("Type the number of year that the money will be compounded for:"))
formula = p*(1+(r/n))**(n*t)
return formula
print (calculate())
So you need to return a value since you are writing a function. I would say input all your values into the function directly like so:
def calculate(P, r, n , t):
exponent = n*t
parends = 1 + (r/n)
val = parends ** exponent
ans = P * val
return ans
print(calculate(10000, .08, 12, 1))
I would check out other resources to learn how to use functions. Codeacademy is a good one.
Here is the function just not broken into pieces:
def shorter(P, r, n, t):
return P*(1+(r/n))**(n*t)
print(shorter(10000, .08, 12, 1))
I am supposed to code this pattern:
[1]
[0][0]
[1][1][1]
[0][0][0][0]
[1][1][1][1][1]
[0][0][0][0][0][0]
[1][1][1][1][1][1][1]
[0][0][0][0][0][0][0][0]
[1][1][1][1][1][1][1][1][1]
However, i am only able to give a output like this:
[1]
[0][0]
[1][1][1]
[0][0][0][0]
[1][1][1][1][1]
[0][0][0][0][0][0]
[1][1][1][1][1][1][1]
[0][0][0][0][0][0][0][0]
[1][1][1][1][1][1][1][1][1]
[0][0][0][0][0][0][0][0][0][0]
this is my current code:
a = '[1]'
b = '[0][0]'
for c in range(1,10,2):
print(a)
a = a + '[1][1]'
print(b)
b = b + '[0][0]'
Please help! I am supposed to the pattern with a while loop and for loop. Thanks in advance!
You're printing both a and b in every iteration, so naturally you can't get odd number of lines in your output.
Instead, use a normal range iterator of 9 and alternate between printing and appending to a and b depending on the counter's remainder of 2.
a = '[1]'
b = '[0][0]'
for c in range(9):
if c % 2 == 0:
print(a)
a = a + '[1][1]'
else:
print(b)
b = b + '[0][0]'
This outputs:
[1]
[0][0]
[1][1][1]
[0][0][0][0]
[1][1][1][1][1]
[0][0][0][0][0][0]
[1][1][1][1][1][1][1]
[0][0][0][0][0][0][0][0]
[1][1][1][1][1][1][1][1][1]
I am trying to convert a list of integers in Python into a single integer say for example [1,2,3,4] to 1234(integer). In my function, I am using following piece of code:
L = [1,2,3,4]
b = int(''.join(map(str, L)))
return b
The compiler throws a ValueError. Why so? How to rectify this issue?
You can do this like this also if that cause problems:
L = [1,2,3,4]
maxR = len(L) -1
res = 0
for n in L:
res += n * 10 ** maxR
maxR -= 1
print(res)
1234
another solution would be
L = [1,2,3,4]
digitsCounter = 1
def digits(num):
global digitsCounter
num *= digitsCounter
digitsCounter *= 10
return num
sum(map(digits, L[::-1]))
the digits() is a non pure function that takes a number and places it on place value depending on the iteration calling digits on each iteration
1. digits(4) = 4 1st iteration
2. digits(4) = 40 2nd iteration
3. digits(4) = 400 3rd iteration
when we sum up the array returned by map from the inverted list L[::-1] we get 1234 since every digit in the array is hoisted to it place value
if we choose not no invert L array to L[::-1] then we would need our digits function to do more to figure out the place value of each number in the list so we use this to take adv of language features