discord.py strike system, assign variable inside command - discord.py

#bot.command()
async def strike(ctx, member : discord.Member):
count = 0
if (member.id == <id here>) and (count < 3):
count = count + 1
i am pretty new to python in general and have only used it to create a discord bot. When the strike command is run, it sets count = to 0 and therefore the count variable cannot go higher than 1. If I move the variable assignment outside of the command I get an error saying that the variable was referenced before it was assigned.
How can I change my code to add 1 to the variable every time the command is run without reassigning the variable every time the command is run?
I removed a lot of the code so the question is less cluttered, I believe this is the only issue preventing this command from working.

You need to make your variable as a global variable.
bot.count = 0
#bot.command()
async def strike(ctx, member : discord.Member):
if (member.id == <id here>) and (bot.count < 3):
bot.count += 1
You can also do:
count = 0
#bot.command()
async def strike(ctx, member : discord.Member):
global count
if (member.id == <id here>) and (count < 3):
count += 1
I don't know what are you trying to do here but I'm only fixing your code

Related

I am not getting the desired output with my python code

<code>def CheckNumber(MyList, number):
counter=0
while number!=0:
for i,element in enumerate(MyList):
if number%10==element:
del MyList[i]
else:
continue
number = number/10
if len(MyList)==0:
return 1
else:
return 2
print("Program to print all the possible combinations of a number")
MyNumber = int(input("Enter the number: "))
MyList = []
while MyNumber!=0:
MyList.append(MyNumber%10)
MyNumber=int(MyNumber/10)
MyLimit = 10**(len(MyList)-1)
for i in range(MyLimit, MyLimit*10):
answer = CheckNumber(MyList, i)
if answer == 1:
print(i)
else:
continue`</code>
I am a beginner at programming and I was trying to write a code to print all the possible combinations of a number. If user enters a 3 digit number the program will check all the three digit numbers to find possible combinations but instead it gives all the numbers as output. For example if user enters 12 then the output should be 12 21 but instead it shows every number from 10 to 99.
As far as I know everything is working fine but the results are not as I expect.
This is a pass-by-reference vs pass-by-value problem. What that means is when you pass a list to a function in python you are not passing the values in that list, you are passing the list itself, or rather its location in memory. So when you are modifying MyList in your CheckNumber function you are actually modifying the MyList variable globally. This is not true for primitive types which is why modifying number does not change i in the for loop. Quick example:
def foo(my_list):
my_list.append('world')
print(my_list)
a = []
foo(a) # this will print out 'world'
print(a) # this will print out 'world'
b = 'hello'
foo(b.copy()) # This will print out 'hello world'
print(b) # Here we have not passed b directly into foo,
# but instead passed a copy, so this will just print out 'hello' as b
# has not been modified
To summarize variable are stored in a specific location in memory. When you pass-by-reference you are passing a long that location in memory so you variable will be mutated. If you pass-by-value, you function will create a new variable and store a copy of the data so you will not mutate your outer variable. In other languages you can specify which way to pass in a variable but afaik you cannot in python.
With that out of the way this is a very easy fix. You don't want to modify your original MyList so just make a copy of it and pass that into the function. You also forgot to cast number/10 to int in the CheckNumber function. The working code should look like this:
def CheckNumber(MyList, number):
counter=0
while number!=0:
for i,element in enumerate(MyList):
if number%10==element:
del MyList[i]
else:
continue
number = int(number/10)
if len(MyList)==0:
return 1
else:
return 2
print("Program to print all the possible combinations of a number")
MyNumber = int(input("Enter the number: "))
MyList = []
while MyNumber!=0:
MyList.append(MyNumber%10)
MyNumber=int(MyNumber/10)
MyLimit = 10**(len(MyList)-1)
for i in range(MyLimit, MyLimit*10):
answer = CheckNumber(MyList.copy(), i)
if answer == 1:
print(i)
else:
continue
More info on pass-by-reference:
What's the difference between passing by reference vs. passing by value?
https://blog.penjee.com/passing-by-value-vs-by-reference-java-graphical/
https://courses.washington.edu/css342/zander/css332/passby.html

How To Get The Member Count Discord.py

I am trying to add a status in my discord bot, which shows the total number of members, the bot is watching like all bots show for exmaple.
If my bot is added in 2 servers, 1st one has 20 members, second one has 30 members.
= 50 Members Total, How Can I show this???? Can anybody help??
Proof That The Code Is 100% Working
def find_members():
l = str(client.guilds)
l = l.split(" ")
f = 0
for i in range(len(l)):
if "member_count=" in l[i]:
s = l[i]
d = ""
for i in s:
if i.isnumeric():
d += i
else:
pass
f += int(d)
else:
pass
return f
Your function can be simplified significantly. Guild has a member_count property which is the number of members in the guild. You do have to have the members intent enabled for that though. But using that, getting the member count is trivial.
There's also no need to make the list of guilds a list and then make it a list again - just iterate over it directly. With Python, we can iterate over the items in a list doing for thing in things so we don't need to use range here either; there's no real need to know the index of the guild in the list.
def find_members(client: discord.Client) -> int:
total_member_count = 0
for guild in client.guilds:
total_member_count += guild.member_count
return total_member_count
You can now call the find_members function and pass in the client (or remove it and rely on the client already defined in the file).
I've found a solution which you can use to get the total count of members a bot is watching
def find_members():
l = str(client.guilds)
l = l.split(" ")
f = 0
for i in range(len(l)):
if "member_count=" in l[i]:
s = l[i]
d = ""
for i in s:
if i.isnumeric():
d += i
else:
pass
f += int(d)
else:
pass
return f
For Proof Checkout The Image URL In The Question!
so there is a easy method by using bot.users function, the only thing you need is to enable server member intent on the discord bot dashboard and then just add this piece of code,
len(bot.users)

How to detect number in certain range

Trying to get bot to send a message when the person sends a message in range
async def on_message(message):
if 0 < Message < 100 in message.content:
await bot.message.send(message.channel, "you are in Bronze 1")
Ok first of all. Please don't code on mobile. Especially not python. That will mess things up way too much.
Second, please define your variables properly. Message is not a defined variable. It will return an error.
So, as you said, you wanted two ways to do this. The number should be the message itself, or within the message.
For the first example, all you have to do is cast the message.content to int. Then you can check if it's in the range.
if 0 < int(message.content) < 100:
...
For the second example, you will have to do something similar, however, you should split the entire message.content string and convert the number ones into integers. I'm assuming that the number will not be within a word and it will be by itself.
for word in message.content.split():
if word.isnumeric():
num = int(word)
if 0 < num < 100:
... # return and send message to avoid spamming

Ruby Recursion Counter Without Global Variable

I am trying to count the number of times the method recurses during the life of the program. The code below gets the desired result, but uses global variables. Is there a way around this or a better way?
$count = 0
def AdditivePersistence(num)
return 0 if num.to_s.length == 1
numarr = num.to_s.chars.map!(&:to_i)
i = numarr.inject(&:+)
$count+=1
if i.to_s.length!=1
AdditivePersistence(i)
end
$count
end
Since you want the total number of recursive calls during the lifetime of the program, a global variable in some form is the only way you can do it. You can either use an explicit global variable, as you have done, or a global variable in disguise, such as a singleton class, or a thread-local variable. I will not illustrate those here since they are inferior to plain global variables for this use case.
You could take in an array with the first variable in the array being num and then the second being the count. then you just will do return [num, count]
Another option would be to update your method definition to accept the counter as an argument.
Using this approach, your method can just increment whatever counter value it receives and then pass the incremented value along in the recursive call.
def AdditivePersistence(num, counter)
return 0 if num.to_s.length == 1
numarr = num.to_s.chars.map!(&:to_i)
i = numarr.inject(&:+)
counter +=1
if i.to_s.length!=1
AdditivePersistence(i, counter)
end
counter
end
# usage
AdditivePersistence(12, 0)

What is the most optimized method to loop through this code?

stocks is a dict():
stocks[0]: [u'portfolio1', u'Active']
stocks[1]: [u'portfolio2', u'Active']
stocks[2]: [u'portfolio3', u'Inactive']
I am trying to check the status of the portfolio which is stocks[0][1], stocks[1][1] and stocks[2][1] and create a list of elements containing only the active portfolio.
And, I am using a counter to do the iteration which seems to be a very slow process. What is the most efficient method to loop through this code?
a = 0
test = {}
while a <= 500:
try:
if stocks[a][1] == 'Active':
test[a] = stocks[a][0]
print test[a]
a +=1
else:
pass
a +=1
except KeyError:
break
test = list(test.values())
test = str(','.join(test)).split(',')
One thing you could try is instead of using a counter is to iterate through the dictionary values themselves, returning only those portfolios with a status of Active. When you find yourself needing to check all of the items in a particular data structure, it is usually easiest to iterate over the structure itself instead of using a counter (i.e. saying for item in iterable instead of for x in range(len(iterable)): iterable[x]):
In [1]: stocks = {
...: 0: [u'portfolio1', u'Active'],
...: 1: [u'portfolio2', u'Active'],
...: 2: [u'portfolio3', u'Inactive']
...: }
In [2]: actives = [x[0] for x in stocks.itervalues() if x[1] == 'Active']
In [3]: actives
Out[3]: [u'portfolio1', u'portfolio2']
actives in this cause is generated using a list comprehension that iterates through the values of the stocks dictionary and returns only those where x[1] (the status, in your case) is equal to Active.

Resources