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
Write a program that prompts the user for their name. Your program should print a statement that greets that user.
$ python3 practice_1.py
What is your name? Sheila
Hello Sheila
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.
$ python3 practice_2.py What is the value of operand 1? 5 What is the value of operand 2? 9 14 $
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.
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.
$ python3 handshakes.py How many students do you have? 28 It would take 378 handshakes for all 28 students to meet each other.
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:
which is 1 handshake.
If there are 3 people, Alice, Bob, and Carol, the following pairs will need to shake hands:
which is 3 handshakes.
If there are 4 people, Alice, Bob, Carol, and Dan the following pairs will need to shake hands:
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?
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 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.
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.
$ 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.
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.
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.
$ 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.