Can I get some suggestion on the error in my code?

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 722 Views - Last Post: 04 October 2012 - 02:52 AM Rate Topic: -----

#1 kehara15  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 13-September 12

Can I get some suggestion on the error in my code?

Posted 03 October 2012 - 06:26 PM

I have this code written but I keep getting an error.

Can I get some suggestion on my code?


def fun1(myNumList):
    for element in myNumList:
        num1 = element[1]
        num2 = element[2]
        newElement = [num1, num2* num1]
        
        print(newElement) 

 
def fun2(listw):

    for i in range(len(listw)):
       
        print("\nList "+ str(i))

        for n in range(len(listw)):

            print(listw[i][count])
            count=count+1

def main():

    myNumList = [[0,1], [1,2], [2,3], [3,4]]  
    list1=[["MATH","BIO","CS"],[10,15,20],["A","B","C"]]

    fun1(myNumList) 
    fun2(list1)

main()


Is This A Good Question/Topic? 0
  • +

Replies To: Can I get some suggestion on the error in my code?

#2 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,056
  • Joined: 28-December 10

Re: Can I get some suggestion on the error in my code?

Posted 03 October 2012 - 06:38 PM

What error do you get?
Was This Post Helpful? 0
  • +
  • -

#3 kehara15  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 13-September 12

Re: Can I get some suggestion on the error in my code?

Posted 03 October 2012 - 06:44 PM

Traceback (most recent call last):
File "C:/Users/Kehara Taylor/Desktop/CS177 Files/Lab07.py", line 29, in <module>
main()
File "C:/Users/Kehara Taylor/Desktop/CS177 Files/Lab07.py", line 26, in main
fun1(myNumList)
File "C:/Users/Kehara Taylor/Desktop/CS177 Files/Lab07.py", line 4, in fun1
num2 = element[2]
IndexError: list index out of range
Was This Post Helpful? 0
  • +
  • -

#4 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,056
  • Joined: 28-December 10

Re: Can I get some suggestion on the error in my code?

Posted 03 October 2012 - 06:55 PM

Try using 0 and 1 instead? Index should be 0. Also, there's another error waiting for you because count is used even though it isn't assigned yet.

Use 0 and 1 for the element variable indices.
Was This Post Helpful? 0
  • +
  • -

#5 kehara15  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 13-September 12

Re: Can I get some suggestion on the error in my code?

Posted 03 October 2012 - 07:03 PM

Okay! So that helped me stop getting an error now.

This is my revised code:

def fun1(myNumList):
    for element in myNumList:
        num1 = element[0]
        num2 = element[1]
        newElement = [num1, num2* num1]
        print(newElement) 

 
def fun2(listw):
    
    for i in range(len(listw)):
       
        print("\nList "+ str(i))
        count = 0
        for n in range(len(listw)):
         
            print(listw[i][count])
            count=count+1

def main():

    myNumList = [[0,1], [1,2], [2,3], [3,4]]  
    list1=[["MATH","BIO","CS"],[10,15,20],["A","B","C"]]

    fun1(myNumList) 
    fun2(list1)

main()


My Outputs should be showing this:
[1, 1]
[2, 4]
[3, 9]
[4, 16]

List 0
MATH
BIO
CS

List 1
10
15
20

List 2
A
B
C

But instead I am getting this:
[0, 0]
[1, 2] THIS SECTION DOESN'T MATCH UP!
[2, 6]
[3, 12]

List 0
MATH
BIO
CS

List 1
10
15
20

List 2
A
B
C
Was This Post Helpful? 0
  • +
  • -

#6 kehara15  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 13-September 12

Re: Can I get some suggestion on the error in my code?

Posted 03 October 2012 - 07:13 PM

I changed the line under func1() to this:

Please tell me if this is the best way to do this
newElement = [num1+1, num2* num2]

Was This Post Helpful? 0
  • +
  • -

#7 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,056
  • Joined: 28-December 10

Re: Can I get some suggestion on the error in my code?

Posted 03 October 2012 - 07:17 PM

  num1 = element[0]
        num2 = element[1]
        newElement = [num1, num2* num1]



This is the culprit.

With your current set:
[[0,1], [1,2], [2,3], [3,4]] 
0, 0 * 1 = 0, 0
1, 2 * 1 = 1, 2
2, 2 * 3 = 2, 6
3, 4 * 3 = 3, 12



What should be the formula to your new element? I think what you need is the second element multiplied by itself?
From what I infer to your expected output, it should print [element2, element2 * element2].
Is that what you wanted?

EDIT:

Well, from your last post, yes, that is also correct. Maybe yours is the correct one, I just printed the second element since it is the same as your expected input anyway. ^^
Was This Post Helpful? 0
  • +
  • -

#8 kehara15  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 13-September 12

Re: Can I get some suggestion on the error in my code?

Posted 03 October 2012 - 07:19 PM

Ok thanks! That does the job too!
Was This Post Helpful? 0
  • +
  • -

#9 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,056
  • Joined: 28-December 10

Re: Can I get some suggestion on the error in my code?

Posted 03 October 2012 - 07:23 PM

No problem, use whatever you think will fit your expected behavior.
Was This Post Helpful? 0
  • +
  • -

#10 kehara15  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 13-September 12

Re: Can I get some suggestion on the error in my code?

Posted 03 October 2012 - 07:30 PM

Ok I am working on another code!

These are the instructions:
Write a function StudentGPA.
1) One parameter: list of GPAs
2) Create two news lists (listA, listB)
3) Create a loop similar to Example 5-b from Prelab7.
4) Inside the loop you need to check if the GPA is greater or equal to 3.7 (append to listA)or if it is greater or equal to 2.7 and less or equal to 3.6 (append to listB)
5) Return listA and listB


And this is this is the code I started:

def StudentGPA([listA,ListB]):
    listA = []
    listB = []
    for i in range(len()):
        
    print ()
    return listA, listB


Can you tell me if I am on the right track?
Was This Post Helpful? 0
  • +
  • -

#11 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,056
  • Joined: 28-December 10

Re: Can I get some suggestion on the error in my code?

Posted 03 October 2012 - 07:40 PM

Too soon to tell, since there's no processing in the function yet. How about this, try running it first when you get it working then if something's wrong, let's check it out. ^^
Was This Post Helpful? 0
  • +
  • -

#12 kehara15  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 13-September 12

Re: Can I get some suggestion on the error in my code?

Posted 03 October 2012 - 08:02 PM

I am actually extremely lost on what my next step should be.
Was This Post Helpful? 0
  • +
  • -

#13 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,056
  • Joined: 28-December 10

Re: Can I get some suggestion on the error in my code?

Posted 03 October 2012 - 08:17 PM

How would you get the GPA? The segregation part is really simple:
Get GPA
If GPA is greater than 3.7
 append to first list
else if GPA greater than or equal to 3.6 and greater than 2.7
 append to second list
else 
 dump grade


Was This Post Helpful? 0
  • +
  • -

#14 kehara15  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 13-September 12

Re: Can I get some suggestion on the error in my code?

Posted 03 October 2012 - 08:26 PM

This is what I have but it is giving me an error at ListA.append()

def StudentGPA(listA,ListB):
    listA = []
    listB = []
    for i in range(len()):
        GPA =eval(input("What is the GPA?")
        if (GPA >= 3.7)
            listA.append()
        else if (GPA >= 2.7 or GPA <= 3.6):
            listB.append()
        else:
            print(GPA)
    return listA, listB

Was This Post Helpful? 0
  • +
  • -

#15 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,056
  • Joined: 28-December 10

Re: Can I get some suggestion on the error in my code?

Posted 03 October 2012 - 08:49 PM

It is because to append you need to put in what you need to add, so put in a parameter there inside append. ^^
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2