Posts: 4
Threads: 1
Joined: Dec 2017
arr=[1,2,3,9]
ip=arr[0]
im=arr[3]
srcfor=ip+im
i=8
while srcfor>=0:
if srcfor<8:
ip+=1
srcfor=ip+im
if srcfor==i:
break
else:
if srcfor<8:
im-=1
srcfor=ip+im
if srcfor==i:
break
print (srcfor)
Hi, I'm new of the Forum. The code that you can see at the top of the thread is a my experiment. I was searching to create a code that into an array search a couple of numbers that if sum, the result would be 8. So I tried with some while loops and if statements, but it don't give me any output, Please Help Me.
Posts: 1,150
Threads: 42
Joined: Sep 2016
Dec-20-2017, 10:23 PM
(This post was last modified: Dec-20-2017, 10:23 PM by j.crater.)
Hello and welcome to the forums!
You get no result because your while loop runs indefintely. srcfor never goes below 0, and also neither of the conditions within the while loop never gets True.
In the while loop, both if (line 7) and else (line 12) check srcfor < 8. I would guess this is a typo, and you probably wanted one of those checks to be srcfor > 8 or srcfor >= 8 (one of comparisons should also check for equality with 8, otherwise you don't get the case of srcfor == 8 covered).
It will work to put another if statement inside an else clause (like you have in line 13). But it doesn't make too much sense, because you only check for two possible outcomes. So else should be just any other case, no need for another if inside it.
Posts: 4
Threads: 1
Joined: Dec 2017
And why if I change the code in this
def couple_searching(arr,srcfor,ip,im,i):
while ip<5 or im>=0:
if srcfor==i:
print ("Good, I've found it !")
break
else:
if srcfor<i:
ip+=1
if srcfor<i:
srcfor=arr[ip]+arr[im]
else:
im-=1
if srcfor>i:
srcfor=arr[ip]+arr[im]
if ip>=5 and im<0:
print ("I haven't found it")
arr=[1, 2, 3, 9]
i=8
ip=0
im=3
srcfor=arr[ip]+arr[im]
couple_searching(arr,srcfor,ip,im,i) In line 14 it gives me the error "Index out of range" ?
Posts: 1,150
Threads: 42
Joined: Sep 2016
Because value of either ip or (more likely) im has gone beyond 4, which is size/length of your list arr. Bear in mind that in Python, list indexes start with zero. So in your case: arr[0] is 1, arr[1] is 2 etc...
Posts: 4
Threads: 1
Joined: Dec 2017
Thank you very much for the helping; now my code work !! If you want, you can run it and try !
def couple_searching(arr,srcfor,ip,im,i):
while ip<4 or im>=0:
if ip<im:
if srcfor==i:
print ("Good, I've found it !")
break
elif srcfor<i:
if ip<im:
ip+=1
if ip<im:
srcfor=arr[ip]+arr[im]
else:
if srcfor>i:
if ip<im:
im-=1
if ip<im:
srcfor=arr[ip]+arr[im]
else:
print("I'haven't found it")
break
arr=[1, 5, 4, 9]
i=8
ip=0
im=3
srcfor=arr[ip]+arr[im]
couple_searching(arr,srcfor,ip,im,i)
Posts: 4
Threads: 1
Joined: Dec 2017
Jan-11-2018, 07:26 PM
(This post was last modified: Jan-11-2018, 07:26 PM by Lorenzo173.)
import tensorflow
from sklearn import tree
features = [[140, "smooth"], [130, "smooth"] [150, "bumpy"], [170, "bumpy"]]
labels = ["apple", "apple", "orange", "orange"]
clf = tree.DecisionTreeClassifier()
clf = clf.fit(features, labels)
print (clf.predict([[150, 0]])) Here, when I run the code the console give me an Error:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-13-a7625f5ebe93> in <module>()
----> 1 features = [[140, "smooth"], [130, "smooth"] [150, "bumpy"], [170, "bumpy"]]
2 labels = ["apple", "apple", "orange", "orange"]
3 clf = tree.DecisionTreeClassifier()
4 clf = clf.fit(features, labels)
5 print (clf.predict([[150, 0]]))
TypeError: list indices must be integers or slices, not tuple Do you kow why it give me this result?, if yes please help me.
Change=#Sorry very much, I've understand my error, I'm studipid
|