Lab 4



Exercise 1.

The following is a code that computes the real roots of the quadratic equation. Copy it to your directory. Lets call it
#! /usr/local/bin/python3
import math
import sys
# Compute the real roots of the quadratic equation 

# Ask the user for a, b, and c and get them 
print("Solving the quadratic equation ax^2 + bx + c")
a, b, c = eval(input("Enter a, b, c "))

# Compute the "discriminant"
d = b*b - 4*a*c

# Get the sign of d 
signd = 1

if (d < 0):
    signd = -1
    signd = 1

if (signd > 0): 
    x1 = -b + math.sqrt(d)/2*a
    x2 = -b - math.sqrt(d)/2*a
    # 1,5,6
    print("The roots are", x1, " ", x2)

    # a=4, b=2, c=4
    print("The roots are complex")
Lets go through it line by line
  • python3
  • import
  • hash tag
  • input list
  • condition statement
  • arithmatic operation
  • exiting
  • Exercise 2.

    Last class we learned about for loop and the range function. To quickly re-cap.
  • for i in range(N): statement else: statement
  • range(N) = 0,1,2,3,4,..,N-1
    range(8,N) = 8,9,10,11,...,N-1
    range(8,N,2) = 8,10,12,14,...,N-1
    We could accomplish the same thing with the while loop.
  • i=0 # initialize i while i < N: statement i = i + 1 #increment

  • Here the loops is executed as long as the condition after the while loop is true. for example try the following lines and run your code:
    i=0 # initialize i 
    while (i < 5):
        print (i) 
        i = i + 1 #increment

    Now change the initialized value of i to 15.
    i=15 # initialize i 
    while (i < 5):
        print (i) 
        i = i + 1 #increment

    This loop will not print out anything. This is because the condition after the while loop is now false. The while loop will only run if the condition is true.

    Note that you need to update the counter yourself (referred to as i in the next example).

    Also notice that here we have to take care to place initialization and incremental statements in the correct place. And it takes three statements to do the job of one for statement. Also note that the loop can go indefinitely as long as the statement after the while loop is true. For example:
    i=0 # initialize i 
    while (i < 5):
        print (i) 
    i=15 # initialize i 
    while (True):
        print (i) 
        i = i + 1 #increment

    For those reasons the for loop is preferable unless we don't know the number of steps needed to solve your calculation.

    Assignment due next Monday by noon

    Problem 1.

  • In Exercise 1 you have made changes to fix the code. Submit your fixed version of the code .
  • Add a condition to the code to exit gracefully would the value of a or c be equals to 0. if a or c is zero the module should not output any root values neither. Submit these changes to exercise one in a file named

    Problem 2.

    Write a python file that will let you know if your number n is even or odd. Where n is an integer command-line argument. Submit a file named The file should print out if the number entered is even or odd.

    Problem 3.

    Write a python script that finds and prints to standard output all prime numbers greater than or equal to 2 and less than n, where n is an integer command-line argument. To run the code.
    Enter n: 20
    where [n] is an arbitrary integer entered by the user.

    The output should look something like:
    the primes less than or equal to 20  are:

    Problem 4 (BONUS).

    You need to find the motion of a falling ball until the point where it hits the ground. However, you don't know how many steps the ball needs to take before it hits the ground. If the position of the ball is y(t) = 1000 - 9.4 * t**2 , using a time step of 0.1 and starting from t=0. Calculate the number of steps needed until the point where the ball hits the ground. Write a python script that calculates and prints out the number of steps in

    Back to main Physics 2235 page