Okay so I for the life of me haven't been able to figure out hot to make this snippet work. My thinking is that since we are working with level in levels we are already in the first dictionary so it should be level[second dictionary]
My current output is an error name not defined. What I am looking to do is to be able to print the name from each dictionary, the hoist value, etc. Ultimately it I will be querying these into statements. Such as print(level[colour]) should print the colour of the current level in a for statement or print(levels[Admin][colour]) should output the colour of admin.
levels={"Admin":{"name":"Admin","hoist":"1","colour":"red"},"Moderator":{"name":"Moderator","hoist":"1","colour":"yellow"},"Henchman":{"name":"Henchman","hoist":"1","colour":"yellow"},"Member":{"name":"Member","hoist":"0","colour":"green"},"Verify":{"name":"Verify","hoist":"1","colour":"white"},"Leach":{"name":"Leach","hoist":"1","colour":"pink"}}
for level in levels:
print(level[name])
Any help is appreciated.
Here is the syntax I am using it in.
#client.command()
async def roles(ctx):
guild=ctx.guild
for level in levels.keys():
name=levels[level]['name']
hoist=levels[level]['hoist']
colour=levels[level]['colour']
await guild.create_role(name=name,hoist=hoist)
Your keys are all strings, so you need to wrap name with quotations.
for level in levels:
print(levels[level]['name'])
print(levels['Admin']['colour'])
Output:
Admin
Moderator
Henchman
Member
Verify
Leach
red
Related
Beginner with Python but I wanted to use a list to contain multiple potential string responses that the user may give in the input.
def Front_Door():
print("Welcome to the Party Friend!")
Emotional_state = input("How are You Today? ")
Positive_Emotion = ["good", "fine", "happy"]
I tried to use an if statement to get python to check through my list to see if the input contained any of the strings listed in the e.g. I gave.
if Positive_Emotion in Emotional_state:
print("That's Great! Happy to have you here!")
The code still manages to prompt me for Emotional_state but it just repeats the question one more time, if I respond with one of then strings I've listed again it gives me this error:
if Positive_Emotion in Emotional_state:
TypeError: 'in <string>' requires string as left operand, not list
I'm guessing there is a method to make Python search through my list of strings and cross reference it with my inputs and give me the response I want?
Any help is appreciated :).
You are checking if the whole list is in the string!
What you probably want to do is check if any of the items in the list are in the string.
Something like:
if any( [emotion in Emotional_state for emotion in Positive_Emotion] ):
print("That's Great! Happy to have you here!")
This will check each emotion in the list, and if any of them are in the string it will return True.
Hope this helps.
I'm all too late to the party, but here are my two cents.
You only need Positive_Emotion and Emotional_state to switch places in your if statement, so that it becomes:
if Emotional_state in Positive_Emotion:
print("That's Great! Happy to have you here!")
Short version:
How do you store functions in a list and only have them be executed when they are called using their index position in the list?
Long Version:
So I am writing a program that rolls a user-chosen number of six-sided dice, stores the results in a list and then organizes the results/ data in a dictionary.
After the data is gathered the program gives the user options from 0-2 to choose from and asks the user to type a number corresponding to the option they want.
After this input by the user, a variable, lets say TT, is assigned to it. I want the program to use TT to identify which function to run that is contained within a list called "Executable_options" by using TT as the index posistion of this function within the list.
The problem I am having is that I have to have the list that contains the functions on a line after the functions have been defined and when I initialize the list it goes through and executes all functions within it in order when I don't want it to. I just want them to be in the list for calling at a later date.
I tried to initialise the list without any functions in and then append the functions individually, but every time a function is appened to the list it is also executed.
def results():
def Rolling_thunder():
def roll_again():
The functions contains things, but is unnecessary to show for the question at hand
Executable_options = []
Executable_options.append(results())
Executable_options.append(Rolling_thunder())
Executable_options.append(roll_again)
options = len(Executable_options)
I am relatively new to Python so I am still getting my head around it. I have tried searching for the answer to this on existing posts, but couldn't find anything so I assume I am just using the wrong key words in my search.
Thank you very much for taking the time to read this and for the answers provided.
Edit: Code now works
The () on the end of the function name calls it - i.e. results() is the call to the results method.
Simply append to the list without the call - i.e:
Executable_options.append(results)
You can then call it by doing e.g.:
Executable_options[0]()
as per your given data the code will look like this:
def results():
def Rolling_thunder():
def roll_again():
Executable_options = []
Executable_options.append(results)
Executable_options.append(Rolling_thunder)
Executable_options.append(roll_again)
for i in range(0,len(Executable_options)):
Executable_options[i]()
this will work for you.
I am trying to add the current dictionary to a Redis list using a dictionary comprehension and then to print out the first (aka current) keys and values of that list. I say current because this is a process I will be continuing with a while loop to have the list building over time, but I have to always access the first keys/values.
I am sure I am totally butchering this, but this is what I have:
adict = {"a":1,"b":2,"c":3}
{rserver.rpush("list",value) for value in adict}
print(float(rserver.lindex("list",0)))
I need to get a list of both keys and values back.
Help would be MUCH appreciated. Thanks!
I am not quite positive on what your redis-list should contain (please include your expected result in the question), but assuming it should at the end of inserts look something like this ["a:1", "b:1", "c:1"], you can achieve this with
adict = {"a":1,"b":2,"c":3}
for key,value in adict.items():
rserver.rpush("list", ":".join([key, value]))
print(float(rserver.lindex("list",0))) #>>> "a:1"
(as you have not included what interface rserver exactly is, it is a bit hard to guess on its exact behavior)
I am making a program in pygame and I am trying to create with buttons where the user can enter their name. So basically when ever they click on the letter, then it puts that letter into a list. I recreated with about the same variables as in my game and got the same error. So if we can fix this, then I can fix my game. I am using functions so that is why you see the global thing as that is needed. Here is the code. I have looked at other forums btw but nothing that is the same as mine.
import glob
unknown=[]
def bob():
global unknown
a=('a').upper()
unknown=unknown.extend(a)
unknown=','.join(unknown)
bob()
Again, this is basically what is in my code except for the name of the function and the name of the list, other than that it is the same. The name doesn't matter though. Thanks in advance.
this is my first time asking a question so let me know if I am doing something wrong (post wise)
I am trying to create a function that writes into a .txt but i seem to get two very different results between calling it from within a module, and writing the same loop in the shell directly. The code is as follows:
def function(para1, para2): #para1 is a string that i am searching for within para2. para2 is a list of strings
with open("str" + para1 +".txt", 'a'. encoding = 'utf-8') as file:
#opens a file with certain naming convention
n = 0
for word in para2:
if word == para1:
file.write(para2[n-1]+'\n')
print(para2[n-1]) #intentionally included as part of debugging
n+=1
function("targetstr". targettext)
#target str is the phrase I am looking for, targettext is the tokenized text I am
#looking through. this is in the form of a list of strings, that is the output of
#another function, and has already been 'declared' as a variable
when I define this function in the shell, I get the correct words appearing. However, when i call this same function through a module(in the shell), nothing appears in the shell, and the text file shows a bunch of numbers (eg: 's93161), and no new lines.
I have even gone to the extent of including a print statement right after declaration of the function in the module, and commented everything but the print statement, and yet nothing appears in the shell when I call it. However, the numbers still appear in the text file.
I am guessing that there is a problem with how I have defined the parameters or how i cam inputting the parameters when I call the function.
As a reference, here is the desired output:
‘She
Ashley
there
Kitty
Coates
‘Let
let
that
PS: Sorry if this is not very clear as I have very limited knowledge on speaking python
I have found the solution to issue. Turns out that I need to close the shell and restart everything before the compiler recognizes the changes made to the function in the module. Thanks to those who took a look at the issue, and those who tried to help.