Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I want the code to count ", !, ?, , but when I run the code it counts all characters typed. Could anyone tell me where I messed up?
def how_eligible():
total = 0
x = ('"','!','?',',')
y = raw_input('Write your essay here.')
for y in x:
if y in x:
total = total + 1
print total
Incase want to know.. A Pythonic 1 liner solution
x = ('"','!','?',',')
y = input('Write your essay here.')
len([i for i in y if i in x])
I would change your double for loop as
for c in y:
if c in x:
total = total + 1
You can also use Counter from the collections module:
from collections import Counter
in_s = 'abc?c?"!'
need = ['"', '!', '?']
char_count = Counter(in_s)
for c in need:
print(c, char_count[c])
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I'm writing a program that takes a list of floating point numbers representing distance in inches and converts them to shoe sizes to the nearest 0.5.
I'm trying to compare an empty list and expect the output to be The list is empty. Instead I don't return any value. Not sure where I'm going wrong?
foot_length = []
for length in foot_length:
if length == 0:
print('The list is empty.')
else:
convert_to_shoe_size = length * 3 - 23
round_shoe_size = round(convert_to_shoe_size * 2, 2) / 2
print(round_shoe_size)
I've tried a number of different approaches none of which get the output I want/expect.
if not length:
print('The list is empty.')
if len(length) == 0:
print('The list is empty.')
Try to change your code to
foot_length = []
if len(foot_length) == 0:
print('The list is empty.')
else:
for length in foot_length:
convert_to_shoe_size = length * 3 - 23
round_shoe_size = round(convert_to_shoe_size * 2, 2) / 2
print(round_shoe_size)
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I am new to python. so any help will be appreciated.
I have two arrays A = [1,2,4,2,3,5,3] and B = [0,4,4,4,1,1,1]
for the function if I give A, B as input then I should get output as = [1,(2+4+2),(3+5+3)] = [1,8,11](if numbers are repeating in B then corresponding values in A should be added together).
This should do the trick:
def bla(list1:list, list2:list):
prev = list2[0] - 1
final_list = []
for ele, pos in zip(list1, list2):
if prev != pos:
final_list.append(ele)
else:
final_list[-1] += ele
prev = pos
return final_list
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
Why is this invalid syntax?
if 0.9*x < d[o] < 1.1*x:
Here's the whole code
def phipsd(d,p):
a=[]
lend = len(d)
ad=np.array(d)
for i in range(0,9):
for o in range(0, len(d)):
x = (500/(2**(i))*10**-6
if 0.9*x < d[o] < 1.1*x:
c = c + p[o]
a.append([])
b=a[i]
b.append(c)
The line you quoted isn't the source of your error. This line is:
x = (500/(2**(i))*10**-6
Note the mismatched parentheses.
def phipsd(d,p):
a=[]
lend = len(d)
ad=np.array(d)
for i in range(0,9):
for o in range(0, len(d)):
x = (500/(2**(i))*10**-6 # Here is a SyntaxError, Because You've started 3 parentheses but terminated only 2. So, add a closing parenthesis in the right place.
if 0.9*x < d[o] < 1.1*x:
c = c + p[o]
a.append([])
b=a[i]
b.append(c)
See the comment
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I want to calculate the variance of values in list x1. Could anyone fix the error in this code?!
def my_mean(L):
s = 0
for i in range(0, len(L)):
s = s + L[i]
return s / len(L)
def my_var(L):
t = 0
for i in range(0, len(L)):
t = t + L[i] - def my_mean(L)
return t*t / len (L)
x1 = [1, 3, 4, -3, 8]
v1 = my_var(x1)
print(v1)
You need to use the def keyword only when you define the function.
When you call to the function you don't need to use def again.
Fix this row:
t = t + L[i] - def my_mean(L)
To:
t = t + L[i] - my_mean(L)
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I'm trying to count the numbers in a given list and only count the even numbers. I keep getting a syntax error and don't know what the issue is.
x = [1,5,4,7,2,10,8,19,27,26,54,80]
def count_evens(g_list):
y = 0
for i in g_list:
if g_list[i] % 2 = 0:
y = y + 1
else:
y = y + 0
print(str(y))
count_evens(x)
The syntax error is coming from if g_list[i] % 2 = 0: What's wrong about my syntax?
Thanks!
syntax error
You want to compare so use == not = (single equal is for assignment)
if g_list[i] % 2 == 0:
index is out of range
To loop through all elements of the list, you can use this form:
for i in g_list:
if i % 2 == 0: # No need for g_list[i]
# in your for loop,
# i is an element from the list, not an index
g_list[i] % 2 = 0 is an assignment statement (and an illegal one at that since you "Can't assign to an operator"). Assignment statements are not allowed in if statements (only expressions).
You want g_list[i] % 2 == 0 which is a logical expression.