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
def getidxmax(window_2, Filtered):
try: bar = window_2[Filtered].idxmax()
return bar
except ValueError:
return np.nan
Am calling the above function like this:
bar = getidxmax(window_2, Filtered)
But am getting error:
File "<algorithm>", line 22
return bar
^
IndentationError: unexpected indent
line 22 is where the try: begins.
Your syntax for try except isn't perfectly correct.
def getidxmax(window_2, Filtered):
try:
return (window_2[Filtered]).idxmax()
except ValueError:
return np.nan
You don't need to create a new variable bar, you can just try and return the result of window_2[Filtered].idmax() and if that fails, np.nan
def getidxmax(window_2, Filtered):
try:
bar = window_2[Filtered].idxmax()
return bar
except ValueError:
return np.nan
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 11 months ago.
Improve this question
from replit import db
import os
def clear():
del db["hits"]
del db["ab"]
def save():
db["hits"] = hits
db["ab"] = ab
try:
hits = db["hits"]
ab = db["ab"]
avg = hits/ab
print(f"You're currently batting {avg}")
except:
hits = 0
ab = 0
input = input("press ENTER to continue.")
os.system("clear")
if input == clear:
clear()
else:
#the line of code below is the one in question
today_ab = input("How many at bats did you have today?")
ab += today_ab
The last line is where it is wrong.
Python doesn't have separate namespaces for variables and functions. This line
input = input("press ENTER to continue.")
replaces the built-in function input with the result of the call. When you try to call input later, you get the TypeError.
Use a different variable name:
x = input("press ENTER to continue.")
if x == "clear":
clear()
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
When running this program I get this error message: NameError: name 'wordList' is not defined. Can't figure out why. Thanks.
import ahocorasick
def build_actree(wordlist):
actree = ahocorasick.Automaton()
for index, word in enumerate(wordlist):
actree.add_word(word, (index, word))
actree.make_automaton()
return actree
if __name__ == '__main__':
actree = build_actree(wordlist=wordlist)
sent_cp = sent
for i in actree.iter(sent):
sent_cp = sent_cp.replace(i[1][1], "**")
sent = '我草你妈'
wordlist =['我草']
print("屏蔽词:",i[1][1])
print("屏蔽结果:",sent_cp)
`
The first thing you do in your code is :
actree = build_actree(wordlist=wordlist)
But there is no place where wordlist is defined. May be some code is missing.
This is the solution I finally came up with:
import ahocorasick
def build_actree(wordlist):
actree = ahocorasick.Automaton()
for index, word in enumerate(wordlist):
actree.add_word(word, (index, word))
actree.make_automaton()
return actree
if __name__ == '__main__':
# correction : add wordlist initialisation
sent = '我草你妈'
wordlist =['我草']
actree = build_actree(wordlist=wordlist)
sent_cp = sent
for i in actree.iter(sent):
sent_cp = sent_cp.replace(i[1][1], "**")
print("屏蔽词:", i[1][1])
print("屏蔽结果:", sent_cp)
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 trying to learn how to code really simple neural networks from scratch. However I can't seen to get the training right. Whenever I run my while loop it gets stuck in an infinite loop.
Here is my code:
#inputs and targets
x1=[1,0,1]
x2=[0,1,0]
x3=[0,0,1]
inputs=[x1,x2,x3]
targets=[1,0,0,1]
#parameters
np.random.seed(1)
w1 = np.random.random_sample(size = 3)
w1=w1.tolist()
alpha=0.1
itera=0
#activation function
def purelin(z):
return z
#network
def red(i,w1):
n=[]
for inp in i:
a=inp*w1[inputs.index(i)]
n.append(a)
n=sum(n) #sumar inputs*weights (caso sin bias)
a=purelin(n)
return a
#forward propagation
def forw(inputs):
outputs=[]
for i in inputs:
x=red(i,w1)
outputs.append(x)
return outputs
#error
def error(targets, outputs):
e=[]
zip_object = zip(targets, outputs)
for i, j in zip_object:
e.append(i-j)
return e
#backpropagation
def back(e):
w1_=[]
zip_object2 = zip(w1, e)
for i, j in zip_object2:
w1_.append(i-alpha*j)
return w1_
#training
while itera<1000:
outputs=forw(inputs)
e=error(targets,outputs)
w1=back(e)
itera=+1
I know I still have a lot of work to do but I want to solve this problem so I can tune my code and get results. Thank you!!
This is just a minor typing error. At the end of the loop, you aren't incrementing itera by one, but are setting it to +1 .
Just change the last line to itera += 1and it should work fine.
Note: Sometimes it may be helpful to print out variables, even if I would always recommend you to debug your code properly.
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 2 years ago.
Improve this question
I've actually already found a work around for my problem, but I feel as though there is still a better way. I can't seem to grasp why the program thinks I'm dealing with a method vs a float.
If I try to run the program, it says that I'm trying to add together two methods. However, when I run the line:
print(type(my.coins.add_coins()))
It tells me that it has returned a float.
Here's the code:
class Currency:
def __init__(self, pennies, nickles, dimes, quarters):
self.pennies = pennies
self.nickles = nickles
self.dimes = dimes
self.quarters = quarters
def penny_value(self):
return self.pennies * .01
def nickle_value(self):
return self.nickles * .05
def dime_value(self):
return self.dimes * .1
def quarter_value(self):
return self.quarters * .25
def add_coins(self):
return self.penny_value + self.nickle_value + self.dime_value + self.quarter_value
my_coins = Currency(1, 1, 1, 1)
#why does this work?
print(my_coins.penny_value() + my_coins.nickle_value())
#but not this?
print(my_coins.add_coins())
Because you are summing functions; Here is what you want to do:
def add_coins(self):
return self.penny_value() + self.nickle_value() + self.dime_value() + self.quarter_value()
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
What is the correct way to deal with return of function in try block?
Option 1:
def someFnc1() -> bool:
try:
fetchSomeData()
return True
except:
return False
Option 2:
def someFnc2() -> bool:
try:
fetchSomeData()
except:
return False
return True
This is described in part in PEP-8:
Additionally, for all try/except clauses, limit the try clause to the absolute minimum amount of code necessary. Again, this avoids masking bugs.
Yes:
try:
value = collection[key]
except KeyError:
return key_not_found(key)
else:
return handle_value(value)
No:
try:
# Too broad!
return handle_value(collection[key])
except KeyError:
# Will also catch KeyError raised by handle_value()
return key_not_found(key)
Based on this your second version should be considered most pythonic (i.e. the absolute minimum of code inside the try clause).