week02_var_expr_problem
week02_var_expr_problem
1. (Week02 P01.py)
Add additional code to the following program so that:
• the values of the two variables a, b are changed.
– Hint: use multiple assignment (i.e. a,b = b,a)
a = input("Enter a: ")
b = input("Enter b: ")
print("Before swapping:", a, b)
Example of execution:
Enter a: 3
Enter b: 5
Before swapping: 3 5
After swapping: 5 3
8-1
2. (Week02 P02.py)
Add additional code to the following program so that:
• the values of the five variables a, b, c, d, e are changed as in the following
diagram:
– i.e. a’s value is assigned to b, b’s value to c, and so on.
– Hint: use multiple assignment (i.e. a,b,c,d,e = ...)
a = input("Enter a: ")
b = input("Enter b: ")
c = input("Enter c: ")
d = input("Enter d: ")
e = input("Enter e: ")
print("Before swapping:", a, b, c, d, e)
Example of execution:
Enter a: 1
Enter b: 2
Enter c: 3
Enter d: 4
Enter e: 5
Before swapping: 1 2 3 4 5
After swapping: 5 1 2 3 4
8-2
3. (Week02 P03.py)
Add additional code to the following program so that:
• the value of the expression ax2 + bx + c is printed.1
– x2 can be computed by x**2 or x*x
a = input("Enter a: ")
b = input("Enter b: ")
c = input("Enter c: ")
x = input("Enter x: ")
# y = ...
Example of execution:
Enter a: 1
Enter b: -4
Enter c: 7
Enter x: 3
Value of the quadratic formula: 4
1 a = int(a)와
같이 type conversion을 추가로 해줘야 하는 이유는 input(·)은 항상 str type의 값을
주기 때문이다. 예를 들어, 숫자 3을 키보드로 입력하더라도 문자열 "3"이 전달되므로 이를 int("3")과
같이 정수형으로 변환해야 사칙연산이 가능한 것이다.
The code a = int(a) for type conversion is required because input(·) always gives str values. For
example, since a string "3" is given instead of keyboard input 3, the string "3" should be converted
into an int value int("3") to be used in arithmetic.
8-3
4. (Week02 P04.py)
Add additional code to the following program so that it
• reads four integers x1, y1, x2, y2 from keyboard where x1 < x2 and y1 < y2;2
• prints out the area of the rectangle represented by the two corner points.
– ( x1, y1) is the bottom-left corner, and ( x2, y2) is the top-right corner of the
rectangle.
# area = ...
Example of execution:
Enter x1: 1
Enter y1: 2
Enter x2: 5
Enter y2: 7
Area of rectangle: 20
2이
조건을 만족하지 않는 경우에 대해서는 정상적으로 작동하지 않아도 된다. 4주차 수업에서 이런
조건을 확인하게 하는 if-else 구문을 다룬다.
Do not consider inputs that do not satisfy this condition; your code need not run correctly for these
inputs. In week 4, we will cover if-else conditionals to deal with these kinds of conditions.
8-4
5. (Week02 P05.py)
Add additional code to the following program so that it
• reads three float values a, b, c from keyboard
– where a, b, c satisfy the inequalities a+b>c , b+c>a , and c+a>b
• prints the area of the triangle of which side lengths are a, b, c
– The area of a triangle whose sides have length a, b, c is
p a+b+c
s(s − a)(s − b)(s − c) where s =
√ 2
– x (= x0.5 ) can be computed by x**0.5
a = float(input("Enter a: "))
b = float(input("Enter b: "))
c = float(input("Enter c: "))
Examples of execution:
Enter a: 3
Enter b: 4
Enter c: 5
Area of triangle: 6.0
Enter a: 3
Enter b: 5
Enter c: 7
Area of triangle: 6.49519052838329
Enter a: 5
Enter b: 5
Enter c: 4
Area of triangle: 9.16515138991168
8-5
6. (Week02 P06.py)
Add additional code to the following program so that it
• reads two positive integers a and b from keyboard;
• computes a remainder when a is divided by b without using the % operator;3
– use only minus (-), multiplication (*) and quotient (//) operators
– what is the value of 101//16 ?
– what is the value of (101//16)*16 ?
• prints out the remainder.
a = int(input("Enter a: "))
b = int(input("Enter b: "))
# r = ...
print("Remainder:", r)
Example of execution:
Enter a: 101
Enter b: 16
Remainder: 5
8-6
7. (Week02 P07.py)
Add additional code to the following program so that it
• reads a positive float value x from keyboard;
• print out the integer that is nearest to x (반올림)
– what is the value of int(2.4 + 0.5) ?
– what is the value of int(2.5 + 0.5) ?
– what is the value of int(2.6 + 0.5) ?
x = float(input("Enter x: "))
# y = ...
Examples of execution:
Enter x: 2.4
2.4 is rounded off to 2
Enter x: 2.5
2.5 is rounded off to 3
Enter x: 2.6
2.6 is rounded off to 3
8-7
8. (Week02 P08.py)
Add additional code to the following program so that it
• reads an integer F from keyboard
– where F represents a temperature in Fahrenheit(화씨)
• prints the temperature in Celsius(섭씨)
– a temperature F in Fahrenheit corresponds to the temperature
5
C = (F − 32) ·
9
F = int(input("Enter F: "))
print("Temperature in Celcius:", C)
Examples of execution:
Enter F: 95
Temperature in Celcius: 35.0
Enter F: 81
Temperature in Celcius: 27.222222222222225
8-8