(Python) name 'curr' is not defined [closed] - python

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
#socketio.on('disconnect')
def disconnect_details():
for room_num in room_users_counter:
curr = 0
expected_num = room_users_counter[room_num]
emit(f"{room_num}$attendance", broadcast=True, include_self=False)
#socketio.on("here")
def here(_room_num):
global curr
if _room_num == room_num:
curr +=1
Error:
line 246, in here
curr +=1
NameError: name 'curr' is not defined
I don't know why its undefined when I defined by saying curr = 0 at the top

You want nonlocal, not global since curr is a local variable (local to disconnect_details), not a global one.
def here(_room_num):
nonlocal curr
if _room_num == room_num:
curr += 1

Related

Convert text file into dictionary [closed]

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 want to turn a txt file with this format:
valido;válido
invalidos;inválidos
avaliacao;avaliação
nao e;não é
into this with Python:
{'valido': 'válido', 'nao e': 'não é', 'invalidos': 'inválidos', 'avaliacao': 'avaliação'}
My code so far:
final = []
with open("fich_teste.txt", 'r') as file:
for line in file:
key; value = linha.sprit().split(';')
final[key] = value
return final
This returns an error:
builtins.NameError: global name 'key' is not defined
You need to assign values using key, value not key; value and fix some indentation problems along with the final declaration and method name:
def myFunc():
final = {}
with open("fich_teste.txt", 'r') as file:
for line in file:
key, value = line.strip().split(';')
final[key] = value
return final

Invalid syntaxt sum(list) >= 2 [closed]

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 get an invalid syntax error at:
if sum(severe_symptoms_list) >= 2
I have set the values for the severe and moderate symptoms slots to True and False depending on what answer was given by the user to a YES/NO question. What have i done wrong ?
class ActionHowManySymptoms(Action):
def name(self) -> Text:
return "action_how_many_symptoms"
def run (self, dispatcher, tracker, domain):
severe_symptom1 = tracker.get_slot('severe_symptom_one')
severe_symptom2 = tracker.get_slot('severe_symptom_two')
severe_symptom3 = tracker.get_slot('severe_symptom_three')
severe_symptom4 = tracker.get_slot('severe_symptom_four')
moderate_symptom1 = tracker.get_slot('moderate_symptom_one')
moderate_symptom2 = tracker.get_slot('moderate_symptom_two')
moderate_symptom3 = tracker.get_slot('moderate_symptom_three')
moderate_symptom4 = tracker.get_slot('moderate_symptom_four')
severe_symptoms_list = [severe_symptom1, severe_symptom2, severe_symptom3, severe_symptom4]
moderate_symptoms_list = [moderate_symptom1, moderate_symptom2, moderate_symptom3, moderate_symptom4]
True = 1
if sum(severe_symptoms_list) >= 2
dispatcher.utter_response(response="utter_some_severe_symptoms", tracker)
elif sum(severe_symptoms_list) == 1
dispatcher.utter_response(response="utter_one_severe_symptoms", tracker)
else:
dispatcher.utter_response(response="utter_no_severe_symptoms", tracker)
if sum(moderate_symptoms_list) >= 2
dispatcher.utter_message(response="utter_some_moderate_symptoms")
elif sum(moderate_symptoms_list) == 1
dispatcher.utter_message(response="utter_one_moderate_symptoms")
else:
dispatcher.utter_message(response="utter_no_moderate_symptoms")
return[]
Try to code in a more decent presentable manner. It will help you in the long run
if (sum(severe_symptoms_list) >= 2):

global variable python, doesn't run [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 4 years ago.
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.
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.
Improve this question
I try to run this code in python 3.6
def Arrange(num):
global sec
sec=0
def Digit(nmb):
return nmb%10
def WithoutTheLastDigit(nmb2):
return nmb2//10
def IsEven(even):
if even%2==0:
return True
else:
return False
def AddDigit(number,dig):
number=number*10+dig
while num>0:
Digit(num)
if IsEven(Digit(num))==True:
sec=sec+AddDigit(sec,Digit(num))
WithoutTheLastDigit(num)
print(sec)
and it shows this error:
>>> Arrange(500)
Traceback (most recent call last):
File "", line 1, in
Arrange(500)
File "C:\Users\Yair\Desktop\hw3.py", line 56, in Arrange
sec=sec+AddDigit(sec,Digit(num))
TypeError: unsupported operand type(s) for +: 'int' and 'NoneType'
Your problem is that the function AddDigit() returns None:
def AddDigit(number,dig):
number=number*10+dig
# this return None by default. mussing `return number`
while num>0:
Digit(num)
if IsEven(Digit(num))==True:
sec=sec+AddDigit(sec,Digit(num)) # This is 0 + None
WithoutTheLastDigit(num)
Note that you code can be simplify greatly with a few things. I didn't change the logic, so you might have some errors here.
def Digit(number):
return number % 10
def WithoutTheLastDigit(number):
return number // 10
def IsEven(number):
return number % 2 == 0:
def AddDigit(number, digit):
return number*10 + digit
while number > 0:
digit = Digit(number)
if IsEven(digit):
sec += AddDigit(sec, digit)
print(sec)

python oop classes, syntax error [closed]

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 7 years ago.
Improve this question
The following is giving me a syntax error at (def__init__(self, price, max_speed, total_miles_ridden, initial_miles = 0):)
I'm also wondering if I can concatenate like this: self.total_miles_ridden + 10?
^:
class Bike(object):
def__init__(self, price, max_speed, total_miles_ridden, initial_miles = 0):
self.price = price
self.max_speed = max_speed
self.total_miles_ridden = total_miles_ridden
self.initial_miles = initial_miles
def displayinfo(self):
print("The price is ", self.price)
print("This bike's max speed is ", self.max_speed)
print("The total miles is ", self.total_miles_ridden)
def ride(self):
print("Riding ", self.total_miles_ridden + 10) #add 10miles to total
def reverse(self):
print("Reversing " self.total_miles_ridden - 5) #minus 5 miles from total
bike1 = new Bike(200, '25mph')
print bike1
I think you're missing a space between def and init.

Python type showing instance before return but none after [closed]

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 8 years ago.
Improve this question
I have a function used to build a tree from a set of nodes
def buildTree(self, nodeList,):
if len(nodeList) > 1:
nodeList = sorted(nodeList, key=self.getKey)
newNode = self.makeNode(nodeList[0], nodeList[1])
nodeList.pop(0)
nodeList.pop(0)
nodeList.append(newNode)
nodeList = self.buildTree(nodeList)
else:
print type(nodeList[0])
return nodeList[0]
This outputs type 'instance'
from TreeFactory import TreeFactory
nodeList = builder.buildNodeList(
'the quick brown fox jumped over the lazy dog')
nodeHead = builder.buildTree(nodeList)
print type(nodeHead)
When I print the type of the return from the builder, I get "type 'NoneType'" printed out
So why is it a valid instance before the return, but not after?
As per https://stackoverflow.com/users/367273/npe the issue was that the if branch was returning nothing, so the recursion was causing nodeList to be filled with None values. By changing the buildTree function so that it reads as
def buildTree(self, nodeList,):
if len(nodeList) > 1:
nodeList = sorted(nodeList, key=self.getKey)
newNode = self.makeNode(nodeList[0], nodeList[1])
nodeList.pop(0)
nodeList.pop(0)
nodeList.append(newNode)
nodeList = self.buildTree(nodeList)
return nodeList
else:
return nodeList[0]
fixed the issue.

Categories