CPSC120B
Fundamentals of Computer Science I

Lab 4

Input

Practice Problem #1

Write a program that prompts the user for their name. Your program should print a statement that greets that user.

Example

$ python3 practice_1.py
What is your name? Sheila
Hello Sheila
Practice Problem #2

Write a program that stores two integers in variables called operand_1 and operand_2. Your program should compute and print the sum of the values of the operands.

Example

$ python3 practice_2.py
What is the value of operand 1? 5
What is the value of operand 2? 9
14
$

Handshakes

One of the great advantages of a small campus like Roanoke College is that it feels like you know everybody on campus. How long would it take for everybody to meet everybody else on campus. Let's find out.

Details

Create a Python program that prints the number of handshakes it would take for every student in a classroom to shake hands with every other student. Your program should use input to get the number of students in the classroom. This program should use arithmetic operations to perform the calculations necessary. Use variables where appropriate, and follow the course's code conventions.

Example

$ python3 handshakes.py
How many students do you have? 28
It would take 378 handshakes for all 28 students to meet each other.

Hint

If you are not sure how to compute the number of handshakes, try some simple examples:

  • If there are 2 people, Alice and Bob, the following pairs will need to shake hands:

    • Alice and Bob

    which is 1 handshake.

  • If there are 3 people, Alice, Bob, and Carol, the following pairs will need to shake hands:

    • Alice and Bob
    • Alice and Carol
    • Bob and Carol

    which is 3 handshakes.

  • If there are 4 people, Alice, Bob, Carol, and Dan the following pairs will need to shake hands:

    • Alice and Bob
    • Alice and Carol
    • Alice and Dan
    • Bob and Carol
    • Bob and Dan
    • Carol and Dan

    which is 6 handshakes.

Can you figure out how many handshakes there are if there are 5 people? Can you figure out the pattern to finding the number of handshakes for any number of people?

Challenge

The average handshake lasts about 3 seconds. Add code to your program to estimate how long it would take for this many handshakes to occur. Your estimate should be output in days, hours, and minutes.

$ python3 handshakes.py
How many students do you have? 1869
It would take 1745646 number of handshakes for all 1869 students to meet each other.
It would take 60 days 14 hours 42 minutes and 18 seconds for everyone to shake hands.

Voyager 1

Voyager 1 is a space probe that was launched by NASA in 1977. It is currently the farthest man-made object from Earth. It is very likely that Voyager 1 is (or will be) the first man-made object to exit the solar system. Currently, Voyager 1 is approximately 20,361,849,111 km from Earth (And yet, Voyager 1 is still closer to the Sun than any other star in our galaxy!). The Voyager 1 communicates with radio waves, which travel at the speed of light, 299,792,458 m/s. At such a great distance, communications take an appreciable amount of time to reach earth.

Details

In Emacs create a Python program in a file called voyager.py. Since Voyager 1 is constantly moving away from Earth, the program should prompt the user for Voyager's current distance from Earth. The program should then print how long it takes a signal from Voyager 1 to reach the Earth. The calculations should be performed using Python's arithmetic operations and printed nicely labeled and formatted. Be sure to use variables where appropriate, and follow the course code conventions.

Example

$ python3 voyager.py
Enter Voyager 1's distance from Earth in kilometers: 20361849111
It takes 67919.81775272012 seconds for a message from Voyager to reach Earth.

Hint

  • You first need to figure out how many meters Voyager 1 is from Earth. Recall that there are 1000 meters in one kilometer.

  • The speed of light above is given in meters per second. So if you divide the distance in meters by the speed of light you will get how many seconds a signal takes to reach Earth.

Challenge

Astronomers recently discovered a potentially habitable planet orbiting the star of one of our solar system's nearest neighbors, Proxima Centauri. Is this new planet close enough that humans could reach it in a lifetime by travelling at the speed of Voyager? Voyager is moving pretty fast, but Proxima Centauri, is also pretty far away, 4.2 light years from Earth. Modify your program to also print how how long it would take Voyager 1 to reach Proxima Centauri at its current velocity. The Voyager 1's velocity is 61,400 km/h.

Example

$ python3 voyager.py
Enter Voyager 1's distance from Earth in kilometers: 20361849111
It takes 67919.81775272012 seconds for a message from Voyager to reach Earth.
It will take 73787.25656288318 years for Voyager to reach Proxima Centauri.
  

Submission

Please show your source code and run your programs for the instructor or lab assistant. Only a programs that have perfect style and flawless functionality will be accepted as complete.