< Back

Lab 4: Input

As usual, create a directory to hold today's files. All programs that you write today should be stored in this directory.

$ cd ~/cs120/labs
$ mkdir lab4
$ cd lab4


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. Assume that there are 28 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
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

There are roughly 1,869 students on the Roanoke campus. That's a pretty big number, but relatively small compared to other schools. If every student tried to shake every other students' hand, how many handshakes would that be? Add code to your program to caclulate and print the result nicely formatted.


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 19,828,976,290 km from Earth. 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. And yet, Voyager 1 is still closer to the Sun than any other star in our galaxy.

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 Voyager 1's velocity is 61,400 km/h. 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: 19828976290
It takes 66142.3453487946 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

Voyager is moving pretty fast, but our closest neighboring star, Alpha 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.

Example

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