Python Forum
homework help - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: homework help (/thread-23704.html)



homework help - maisaadabbah - Jan-13-2020

hey :)
I am totally a beginner taking a python course for college, ill be happy if anyone can direct me in writing the code.
this is so far what i have already coded.
the homework:
A)write the function RandCoef with no arguments, the function must return a random integer from 1 to 10,
also returns a random list of numbers between 0 and 1 where the length equals to N.
B)write the function RandPolCal that takes the random list from A as an argument, the function must return 2 lists - (1) a list of X in the section [-10,10], (2) a list with the same length of the random polinom's values in each one of the values of the X list.

ive already coded (A) but i dont know how to do (B), i thought about nested function but coudlnt write it

import random as r
import numpy as np
import matplotlib.pyplot as plt

#returns the value of the polinom in a specific point (x)
def PolCal(x,li):
    val = 0
    for i in range(len(li)):
        val+=(x**i)*(li[i])
    return val

def RandCoef():
    N = r.randint(1,10)
    L = [r.random() for i in range(N)]
    return N,L

a,b = RandCoef()
print a
print b



RE: homework help - jefsummers - Jan-13-2020

Couple things to start. The assignment says your PolCal should take one argument, not two. In line 20 you will then call it on b, which is the list you generated in the first part.
Second, whenever you are tempted to use range(len(x)), don't. You can say for i in li and it does the same thing, easier to read, easier to debug. It is also a very good idea to use descriptive variable names. We've come a long way from Fortran where variables starting with "i" through "n" were integers and the rest floats.
PolCal also needs to return two lists. You are returning one single value. Read that part of the assignment again and think about what you want to generate, and then how.