python error-can only join an iterable - python-3.x

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.

Related

IndexError: list index out of range in VSCode but works well in colabs

I'm trying to learn entailments in wordnet by rewriting code from a book. The code works well when I code it in colabs, but when I move it in VSCode, I receive an error message:IndexError: list index out of range
The code is like this:
for action in ['walk', 'eat', 'digest']:
action_syn=wn.synsets(action, pos='v')[0]
print(action_syn, '-->', action_syn.entailments())
with problem indicates in here:
action_syn=wn.synsets(action, pos='v')[0]
Can you please explain to me why this happens and how to resolve it. Thank you.
list index out of range in wn.synsets(action, pos='v')[0] would mean that there is no element [0]. Which means wn.synsets() has returned an empty list.
If you want the code to be more sturdy, check the list length before using it:
synset = wn.sysets(action,pos='v')
if len(myList)==0:
print("Not found")
else:
action_syn = synset[0]
#...your other code
Of course you should be finding entries for "walk" as a verb, so I'd guess wordnet was installed correctly on colab and has not been installed correctly on your local machine (or wherever you are running vscode). I'd start that troubleshooting here: https://www.nltk.org/install.html

kivy switch_to screen on if statement doesn't work

I could REALLY use your help with this one.
I'm trying to make a sort-of voice command operated menu for a toddler's learning app and kivy is giving me a headache
all of my screens are correctly defined and load as intended if the buttons are pressed but the voice commands, even though they register correctly and carry over their variables as intended they don't seem to have the desired effect when asked to act upon ScreenManager when the if statement is fulfilled
def on_enter(self):
....
Command.start()
Command.introMenu()
......
if Command.sel == "shapes":
ScreenManager().switch_to = "shapes"
elif Command.sel == "colours":
ScreenManager().switch_to = "colours"
......
else:
pass
the variable Command.sel is captured from a dependency, defined as a string and carried correctly as far as I can tell from the variables view in debugging
yet even though everything seems to be in order (in fact no error messages appear at all) the desired screen is not called when the if condition is met
what am I doing wrong here???
full code here
(please ignore the Greek bits in the code... it's just strings, imagine it's any other language for that matter...)
thank you!
issue resolved
correct command was
self.parent.current = "your_screen_name"
answer (eventually) found here

Python 3 Tips to Shorten Code for Assignment and Getting Around TextIO

I've been going through a course and trying to find ways to shorten my code. I had this assignment to open a text file, split it, then add all of the unique values to a list, then finally sort it. I passed the assignment, but I have been trying to shorten it to learn some ways to apply any shortening concepts to future codes. The main issues I keep running into is trying to make the opened file into strings to turn them into lists to append and such without read(). If I don't used read() I get back TextIO errors. I tried looking into it but what I found involved importing os and doing some other funky stuff, which seems like it would take more time.
So if anyone would mind giving me tips to more effectively code this that are beginner friendly I would be appreciative.
romeo = open('romeo').read()
mylist = list()
for line in romeo.split() :
if line not in mylist:
mylist.append(line)
mylist.sort()
print(mylist)
I saw that set() is pretty good for unique values, but then I don't think I can sort it. Then trying flip flop between a list and set would seem wacky. I tried those swanky one line for loop boys, but couldn't get it to work. like for line not in mylist : mylist.append(line) I know that's not how to do it or even close, but I don't know how to convey what I mean.
So to iterate:
1. How to get the same result without read() / getting around textIO
2. How to write this code in a more stream lined way.
I'm new to the site and coding, so hopefully I didn't trigger anyone.

Python3 Nested Dictionary print

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

Same for loop, giving out two different results using .write()

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.

Resources