CodeLab CodeLab #1 ----- Language: Python ----- Question: Write some code that r
ID: 3891147 • Letter: C
Question
CodeLab
CodeLab #1
-----
Language: Python
-----
Question: Write some code that reads a value representing a name into the variable name then prints the message "Greetings, NAME!!!" on a line by itself where NAME is replaced the value that was read into name. For example, if your code read in "Hassan" it would print out "Greetings, Hassan!!!" on a line by itself.
Instructor Notes:
If you see a (backslash) in front of the exclamation marks, it shouldn't be there; some browsers seem to erroneously display it.
=====
Question: Assume that price is an integer variable whose value is the price (in US currency) in cents of an item. Assuming the item is paid for with a minimum amount of change and just single dollars, write an expression for the amount of change (in cents) that would have to be paid.
=====
Question: Assume that price is an integer variable whose value is the price (in US currency) in cents of an item. Write a statement that prints the value of price in the form "X dollars and Y cents" on a line by itself. So, if the value of price was 4321, your code would print "43 dollars and 21 cents". If the value was 501 it would print "5 dollars and 1 cents". If the value was 99 your code would print "0 dollars and 99 cents".
=====
Question: The dimensions (width and length) of room1 have been read into two variables: width1 and length1. The dimensions of room2 have been read into two other variables: width2 and length2. Write a single expression whose value is the total area of the two rooms.
=====
Question: In mathematics, the Nth harmonic number is defined to be 1 + 1/2 + 1/3 + 1/4 + ... + 1/N. So, the first harmonic number is 1, the second is 1.5, the third is 1.83333... and so on. Assume that n is an integer variable whose value is some positive integer N. Assume also that hn is a variable whose value is the Nth harmonic number. Write an expression whose value is the (N+1)th harmonic number.
=====
Question: Given two variables, already defined, best_value and second_best_value, write a statement that swaps their associated values.
=====
Explanation / Answer
Solution:
Sample Run:
Testing greeting method.
Enter your name.
Subrat
Hi, Subrat!!!
Testing calculateCents method.
Enter the amount of dollar that needs to be changed in cents.
2
2 dollar is equals to 200 cents.
Testing valueOfPrice method.
Enter the amount to change in dollar and cents.
4321
43 dollars and 21 cents
Testing totalArea method.
Enter the width of room 1.
123
Enter the length of room 1.
100
Enter the width of room 2.
231
Enter the length of room 2.
123
Total Area is 40713
Testing harmonic method.
Enter the value of n to calculate Nth harmonic number.
5
2.283333333333333
Testing swap method.
Enter the value of a.
10
Enter the value of b.
20
After swaping a = 20 and b = 10