Can't edit a tuple from inside a function [closed] - python

Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 7 years ago.
Improve this question
I am programming an application in python. Here is the function:
def aircraftListBoxRefresh():
sqlConnect=sqlite3.connect("fglconfdb")
sqlCursor=sqlConnect.cursor()
sqlCursor.execute("SELECT fgAircraftDir FROM fglconfig")
adl=sqlCursor.fetchall()
global aircraftDirectories
for x in adl:
aircraftDirectories=aircraftDirectories+(x,)
I put print(aircraftDirectories) to test whether the value changes. It changes in side the function. But outside the function it is null.
I am trying to access the value with this:
aircraftDirectories=()
aircraftDir=StringVar(value=aircraftDirectories)
aircraftListBox=Listbox(mainframe,height=7,width=100,listvariable=aircraftDir)
aircraftListBox.place(x=170,y=170)
But I can't.
Any help greatly appreciated.
Regards.

You can't modify tuples anywhere, inside a function or outside, they're immutable. Maybe you want lists:
def aircraftListBoxRefresh():
sqlConnect=sqlite3.connect("fglconfdb")
sqlCursor=sqlConnect.cursor()
sqlCursor.execute("SELECT fgAircraftDir FROM fglconfig")
adl=sqlCursor.fetchall()
for x in adl:
aircraftDirectories.append(x)
aircraftDirectories=[]
aircraftDir=StringVar(value=aircraftDirectories)
aircraftListBox=Listbox(mainframe,height=7,width=100,listvariable=aircraftDir)
aircraftListBox.place(x=170,y=170)
With this approach, since you're modifying the list and not re-assigning to the variable, you don't need global.

You can't modify a tuple, but you can replace it. In this example M[] is a list of tuples, each tuple contains two numbers. I want to replace the first number of a tuple to 0, but keep the second number.
M[j] = (0, M[j][1])

Related

I CANNOT get rid of brackets when trying to print these lists for some reason [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 22 days ago.
Improve this question
I have tried most methods but for some reason it just does not want to print without the brackets. Why is that??
It means that list1 and/or list2 are lists of lists. Try:
for x in list1:
print(x[0])
for y in list2:
print(y[0])
Hey reffering to the first answer which i really liked, it makes you use indexing which i dont really like in my code writing in python.
The complete solution without using any external library or indexing is to use a nested for loop, if it is really a list of lists use the function like so:
def printMe(self):
for list in list1:
for x in list:
print(x)
and the same for the second list,
I was able to repredouce your work and it worked, i wouldve used also more parsing in the strings but it is your own choice,
Hope it helped :)
It is probably caused by an extra axis.
Try this:
for x in list1[0]:
print(x)
If you want to just print the list without square brackets use the command:
print(*x,sep=" ")

Search and Replace a word within a word in Python. Replace() method not working [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 4 years ago.
Improve this question
How do I search and replace using built-in Python methods?
For instance, with a string of appleorangegrapes (yes all of them joined),
Replace "apple" with "mango".
The .replace method only works if the words are evenly spaced out but not if they are combined as one. Is there a way around this?
I searched the web but again the .replace method only gives me an example if they are spaced out.
Thank you for looking at the problem!
This works exactly as expected and advertised. Have a look:
s = 'appleorangegrapes'
print(s) # -> appleorangegrapes
s = s.replace('apple', 'mango')
print(s) # -> mangoorangegrapes
The only thing that you have to be careful of is that replace is not an in-place operator and as such it does not update s automatically; it only creates a new string that you have to assign to something.
s = 'appleorangegrapes'
s.replace('apple', 'mango') # the change is made but not saved
print(s) # -> appleorangegrapes
replace can work for any string, why you think that it doesn't, here is the test:
>>> s='appleorangegrapes'
>>> s.replace('apple','mango')
'mangoorangegrapes'
>>>
Don't you see that you received your expected result?

I need help figuring what wrong with my code! ('int' issue) [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 4 years ago.
Improve this question
i am having an issue figuring out why my code is wrong.
def draw_poly(t, n, size):
for s in (n):
t.forward(sz)
t.left(45)
draw_poly (liz, 8, 50)
I am trying to make a octogon but it keeps on giving me an "'int' object is not iterable" error.
If you could help i would be forever grateful, Thanks.
The for-loop:
for s in (n):
Expects n to be some sort of iterable: a list, tuple, dictionary, etc, but you are passing an integer (int) - hence the error.
If you want s to take the values 0, 1, 2, ..., n then you should use the range() function to produce an iterable sequence of the numbers up to the number passed into it.
Therefore, what your probably want is:
for s in range(n):
which will allow you to work with an integer variable s in that code block.
If you want to debug your code, it often helps to print out the values of variables to check they are evaluating to what you think they should be.
For instance,
for i in range(4):
print(i)
gives:
0
1
2
3
which is to be expected.
The correct int iterable would be range(int), so, use this: for s in range(n):
For the future using of range(): this function creates a list of iterable int objects. In some cases using of xrange() is better, especially for large loops.
I.e. range(1000000000) creates a huge object in memory while xrange(1000000000) does not, though it works in a similar way giving you int numbers one by one.

Python Logical Error in Loop while reading Dictionary [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 5 years ago.
Improve this question
I am new to python and OOPS.I am expecting my module add_book to increment if book is already present in dictionary. Please help me .Not sure why for loop is not working as expected.
https://github.com/amitsuneja/Bookstore/commit/4aefb378171ac326aacb35f355051bc0b057d3be
You should not append to the list while you are still iterating it. Also, your code will append the new item for each item already in the list that has a different name. Instead, you should use a for/else loop. Here, the else case will only be triggered if you do not break from the loop.
for recordlist in self.mybooksinventory:
if self.name == recordlist['name']:
recordlist['quantity'] += 1
break # break from the loop
else: # for/else, not if/else !
self.mybooksinventory.append({'name':self.name,'stuclass':self.stuclass,'subject':self.subject,'quantity':1})

Call the findUniqueDigits function by passing secret as the argument and store the return list.(Say secretUniDigits) [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 6 years ago.
Improve this question
could someone please help me understand what this line is trying to get me to do? Thanks.
findCows function: Write a function called “findCows” that takes two string values as parameters (Say secret, guess) and returns the number of digits in guess match the secret only in digit but not in position. Assume that there are exactly 4 digits in each string. For example, findCows(“1807”, “7810”) will return 3.
You can use filter function:
len(list(filter( lambda x: x[0]!= x[1], zip(secret ,guess))))
def findCows(secret, guess):
sum([x!=y for (x, y) in zip(secret, guess)])
zip(secret, guess) converts two lists of the same size to a list of element-wise pairs. The [x!=y for (x, y) in zip(secret, guess)] statement is a for comprehension which maps each pair to True/False based on their equality. Finally, sum counts the number of True values.

Categories

Resources