I need help figuring what wrong with my code! ('int' issue) [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 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.

Related

Stuck at "list indices must be integers or slices, not tuple" error [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 2 years ago.
Improve this question
I have two lists. Even though the length are the same, the size of them are different. While parameters_train consists of 4 elements in one row, score1 consists of only one.
I am trying to make a function that stores the generalization error. Here's the code and the error.
To give you an idea here is preview of lists.
My expected outcome is this.
generalization_error= [0, 0, 0.06, and so on...
As each element in your x is tuple, when you iterate you get a tuple, not integer, so
you can use enumerate:
for i,_ in enumerate(x):
generalization_error.append((x[i][3] - y[i])
This will ensure you get the right index. If you also need the actual tuple along with it you can do
for i, item in enumerate(x):
generalization_error.append(item[3] - y[i])
You might also need to check your y[i] is a legitimate value, as this comes under assumption that for any given x[i] there's an existing y[i].

Making array of arrays [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
Basically, im trying to make array of arrays in python and i don't know how really. It needs to be in "for" loop and each time it goes trough loop, it needs to add array of some numbers to the array of arrays but of course on index+1
EDIT:
this is what i already tried, but it gives me error:
for x in range(5):
poljeRazina[x][x]=1
In Python, arrays are called Lists. This could be helpful when googling.
Also, you can declare them like this (depth-based):
x = [[]]
Then...
for big in range(10): #outer loop
for small in range(10): #inner loop
x[big].append(small*big) #add new number
print('%03d' % x[big][small], end=" ") #we print the number padded to 3 digits
x.append([]) #add another internal list
print() #move down to next line
Equals...
List comprehension might help
[list(range(i)) for i in range(5)]

Can't edit a tuple from inside a function [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 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])

Writing pseudocode with dummy value [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
An pseudocode is required to input a series of numbers from the keyboard and to ensure that they are in ascending order even though they need not be consecutive.The number may be positive or negative and will end with a dummy value of 9999.
This is a python code I wrote and it works ok,
li=[]
num=int(raw_input("Enter number"))
while num!=9999:
li.append(num)
num=int(raw_input("Enter number"))
new=0
print "Entered sequence is "
print li
j=0
while j<len(li):
for i in range(len(li)-1):
value1=li[i]
value2=li[i+1]
if (value1>value2):
new=li[i]
li[i]=li[i+1]
li[i+1]=new
j+=1
print "ordered list is "
print li
But I have problem in writing this as an algorithm.
This is my tried algorithm:
Main
li list
num integer
new=0,j=0 integer
Begin
num=INPUT num.
while num<>9999.
append num to li.
num=INPUT num.
ENDWHILE.
DISSPLAY "Entered Sequence is".
OUTPUT li.
while j<(length of li).
FOR i=0 to (length of li-2).
value1=i th element of li
value2=(i+1) th element of li
if (value1>value2):
new=value1
value1=value2
value2=new
ENDIF
END FOR
j=j+1
ENDWHILE
DISPLAY "ORDERED LIST IS"
DISPLAY li
END
END
Can I use "list" in an algorithm because I think objects as "list" do not appear in every programming language.And shouldn't algorithm be a general code.Same way is it allowed to use arrays in writing an algorithm
And Is it okay to say "value1=ith element in li"?
And how to show that I am assigning the value entered from keyboard to the variable "num"
(This was originally a comment)
You seem to miss what the point of pseudo code is. Pseudo code is neither standardized nor somewhat defined. In general it is just a code-like representation of an algorithm, while maintaining a high level and readability. You can write pseudo code in whatever form you like. Even real Python code could be considered pseudo code. That being said, there are no thing disallowed in pseudo code; you can even write prose to explain something that happens. For example in the inner-most loop, you could just write “swap value1 and value2”.
This is approximately how I would transform your Python code into pseudo-code. I tend to leave out all language specific stuff and focus just on the actual algorithmic parts.
Input:
list: Array of input numbers
FOR j = 0 to length(list):
FOR i = 0 to length(list)-1:
if list[i] > list[i+1]:
Swap list[i] and list[i+1]
OUTPUT ordered list
So is it okay to use lists, tuples, dictionaries in a pseudocode, even if they are not common to all programming languages?
Absolutely! In more complex algorithms you will even find things like “Get minimum spanning tree for XY” which would be a whole different problem for which again multiple different solutions exist. Instead of specifying a specific solution you are keeping it open to the actual implementation which algorithm is to be used for that. It usually does not matter for the algorithm you are currently describing. Maybe later when you analyze your algorithm, you might mention things like “There are algorithms known for this who can do this in O(log n)” or something, so you just use that to continue.

error<0x275b990> - what does this mean [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have this function, but i'm not familiar with the error or how to correct it.
def intify(file1):
numbers=range(0,10)
strnum=[]
for x in numbers:
strnum.append(str(x))
number1=[]
for line in file1:
for split in line.split(' '):
number1.append(split)
listnum=[]
for x in number1:
if x[0] in strnum:
listnum.append(x)
w=map(float, listnum)
#return w
print(w)
error map object at 0x275b990
error map object at 0x275b990
It is not an error - you just print address of iterator, returned by map. You could print list(w) to make sure that everything's alright. Of course, you should not return list(w) since it is unnecessary and expensive.
In Python 3+, map() is a class, not a function (documentation here). A map instance can be iterated over and each item in the sequence will be processed only as needed ("lazy evaluation"). This way an actual list need never be created, saving time and memory, and if you don't need all the items (e.g., you stop processing when you hit a certain value) then you can save a bunch of function calls, too. In general, then, you will want to do something like this with map():
for num in map(float, listnum):
print(num) # or do something else
If you really do want an actual list, just convert it to one: the list() constructor accepts any iterable, including a map() instance:
w = list(map(float, listnum))
What you're seeing when you print a map object is not an error, but rather the default Python display (repr()) of objects. The code is the id() of the object in hexadecimal, which in CPython is its memory address; this is useful mainly for distinguishing one instance from others. You'll be seeing this a lot, so get used to it.

Categories

Resources