Passing values to a function from within a function in python

2024/11/13 10:22:17

I need to pass values from one function to the next from within the function.

For example (my IRC bot programmed to respond to commands in the channel):

def check_perms(nick,chan,cmd):sql = "SELECT `"+ cmd +"` FROM permissions WHERE nick = '"+ nick +"' and chan = '"+ chan +"'" # This returns 0#sql = "SELECT `restart` FROM permissions WHERE nick = 'Me' and chan = '#mychan'" # this works as intendedcursor.execute(sql)result = cursor.fetchall()for row in result:if (row[0] == 1): # Nick logged in and has permissionreturn 1 else: # nick does not have permissionsreturn 0def com_restart(nick,chan):perm = check_perms(nick,chan,"restart")if (perm == 0): # nick did not have permissionirc.send("NOTICE "+ nick +" :Permission denied.\n")elif (perm == 1): # nick has permissionirc.send("PRIVMSG "+ chan +" :I've been asked to restart myself by "+ nick +".\n")nick = "Me" # This is determined by a bunch of regex splits and such
chan = "#mychan" # This is determined by regex splits as well
com_restart(nick,chan)

When I try this, though, it seems the values do not get passed to the SQL query, so it returns 0.

Thanks for any help you can provide.

EDIT - Added code that I'm working with as it stands right now.

Answer

What do you mean the string "sql" "returns zero" -- a string isn't a function or expression so it doesn't "return" anything. A string is just a literal value.

Are you saying that cursor.execute() returns zero? What is "cursor" equal to? Do you correctly initialize the "cursor" object to something somewhere else?

if "cursor" is a global object you may have to declare it as such, like this:

global cursor

otherwise you won't be able to make changes to it.

Other than that I can't figure out what you're doing or what's wrong.

https://en.xdnf.cn/q/119486.html

Related Q&A

How to make Stop button to terminate start function already running in Tkinter (Python)

I am making a GUI using Tkinter with two main buttons: "Start" and "Stop". Could you, please, advise on how to make the "Stop" button to terminate the already running func…

adding language to markdown codeblock in bulk

My Problem is to add to every single block of code a language in my markdown files. Ive hundreds of files in nested directories. The files have this form: ```language a ```Normal text``` b ```Normal te…

Cant randomize list with classes inside of it Python 2.7.4

I am new to coding and I need some help. Im trying to randomize these rooms or scenes in a text adventure but whenever I try to randomize it they dont even show up when I run it! Here is the script:fro…

calculate the queue for orders based on creation and delivery date, by product group

I have a Pandas dataframe containing records for a lot of orders, one recorde for each order. Each record has order_id, category_id, created_at and picked_at. I need to calculate queue length for each …

Python print with string invalid syntax

I have a rock, paper, scissors code Ive been working on lately (yes, I am a total noob at coding), and I get an Invalid Syntax error with this specific line:print(The magical 8ball reads "Your for…

How to load images and text labels for CNN regression from different folders

I have two folders, X_train and Y_train. X_train is images, Y_train is vector and .txt files. I try to train CNN for regression. I could not figure out how to take data and train the network. When i us…

How to calculate number of dates within a year of a date in pandas

I have the following dataframe and I need to calculate the amount of ER visit Dates with a score of 1 that are one year after the PheneDate for that pheneDate for a given subject. So basically phenevi…

Remove substring from string if substring in list in data frame column

I have the following data frame df1string lists 0 i have a dog [fox, dog, cat] 1 there is a cat [dog, house, car] 2 hello everyone [hi, hello, everyone] 3 …

how to save data in the db django model?

Good day, I cant really understand what Im doing wrong in here. I was using this function base view to store my scrap data in the database with the django model, but now its not saving any more. I cant…

Move existing jointplot legend

I tried answers from a previous question to no avail in Matplotlib 1.5.1. I have a seaborn figure:import seaborn as sns %matplotlib inline import matplotlib.pyplot as plt import numpy as np tips = sns.…