Python Syntax - Maths section


#1

How to create a variable called count_to in script.py


#2

post your code so we can try and sniff out your problem


#3

my code is

addition = 72 + 23
subtraction = 108 - 204
multiplication = 108 * 0.5
division = 108 / 9

print count_to = 95


#4

Do you see what you are doing, you've created all these variables but there is absolutely none that goes by the name of count_to. You can only print somethng when it exists and count_to does't exist in your code. Are you trying to create a function or?

An also you can't include any of these operators = + % - bluntly with a print statement


#5

Thanks for your reply. Could you please guide me how to do I proceed with my problem. How do I write the code?


#6

is there a way one can use the square root operator without having to import from the maths module? I don't feel like relying too much on module usage.


#7

It's actually mandatory for you to import the math module unless you would like to go through the long route and just define your own square root function. Other than that you must import the math module which is quite an easy way to go about square roots.


#8

if you have time could you show me one? I'm a beginner at this. Still in high school. Just trying to see if computer programming suits me. It's been really fun but insofar the lesson regarding def functions are tripping me. Guess I just need to do more practice!


#9

Hi, @johnazteca ,

Since you asked about writing a square root function, here's an example. While you are considering whether you like computer programming, it would be best to complete most of the Python track and then come back to this example later on. There are features here that have not been covered yet, and what you learn about loops, if blocks, and functions later in the track will help you understand this code.

The following square_root function estimates the square root of a number, n. It begins with a guessed estimate, then refines the estimate repeatedly by averaging the estimate and n divided by the estimate. When the value of the estimate stops changing, the function returns the result.

# square_root.py
# May 14, 2016
def square_root(n):
    # use a float type for n
    n = float(n)
    # estimate the square root
    est = n / 2
    prev_diff = max(n, 1)
    while True:
        # assign new_est the average of est and n / est
        new_est = (est + n / est) / 2
        # check how much the estimate has changed
        curr_diff = abs(est - new_est)
        # if the change has stopped decreasing, break and return the result
        if curr_diff >= prev_diff:
            break
        # otherwise keep refinining the estimate
        prev_diff = curr_diff
        est = new_est
    return new_est

for i in range(1, 26):
    print("%f %f %f" % (i, square_root(i), i ** 0.5))

Output:

1.000000 1.000000 1.000000
2.000000 1.414214 1.414214
3.000000 1.732051 1.732051
4.000000 2.000000 2.000000
5.000000 2.236068 2.236068
6.000000 2.449490 2.449490
7.000000 2.645751 2.645751
8.000000 2.828427 2.828427
9.000000 3.000000 3.000000
10.000000 3.162278 3.162278
11.000000 3.316625 3.316625
12.000000 3.464102 3.464102
13.000000 3.605551 3.605551
14.000000 3.741657 3.741657
15.000000 3.872983 3.872983
16.000000 4.000000 4.000000
17.000000 4.123106 4.123106
18.000000 4.242641 4.242641
19.000000 4.358899 4.358899
20.000000 4.472136 4.472136
21.000000 4.582576 4.582576
22.000000 4.690416 4.690416
23.000000 4.795832 4.795832
24.000000 4.898979 4.898979
25.000000 5.000000 5.000000

#10

It might seem as if @appylpye beat me to it! :sweat_smile: