Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Unexpected output
#1
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.

Huh
Reply
#2
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.
Reply
#3
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" ?
Reply
#4
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...
Reply
#5
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)
Big Grin
Reply
#6
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 Smile
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020