Starting from:
$35

$29

Lab Assignment (1) Solution


    1. Given a program called chaotic.py, please modify its input and output format. The former is changed into two input variables; the latter is changed from one column to two columns.

For example, this program illustrates a chaotic function. The input and output format for the new program is shown as follows:

This program illustrates a chaotic function

Enter the first float number between 0 and 1: >>> 0.25

Enter the second float number between 0 and 1: >>> 0.26

input
0.25
0.26
output


1
0.73124999999999996
0.75036000000000003
2
0.76644140625000001
0.73054749456000001
3
0.69813501043853754
0.76770662573321646
4
0.82189581879023044
0.69549933390028873
5
0.57089401919693172
0.82594204073371924
6
0.95539874836420990
0.56067096572112018
7
0.16618672195441300
0.96064423228201989
8
0.54041791206179257
0.14744687593470315

    9 0.968628930299804240.49025454937601765

    10 0.118509010175638770.97462960214932848

    2. If we print two strings simultaneously, there is a space between them.

```python

    • print("hello", "world") hello world
```

Make a little change to the above program to convert the space into an ampersand '&'.

```python

    • print("hello", "world") hello&world

```

Tips:

1


If you don't know how to do it, use the code `help(print)` to get some idea.




    3. Write a function (which means you should use `def`) named `upperToLower`. The parameter is a letter with upper case, what you need is convert the upper case into lower case. You should use the knowledge of ASCII to solve this problem.

e.g.:

```python

    • print(upperToLower('A'))

a

    • print(upperToLower('Z'))

z

```

You don't have to consider the situation if the parameter is not a letter with upper case.




    4. Write a function to decrease the number of decimal places of PI. The parameter is the decimal places of the new PI.

```python

    • print(lessDecimalPlaces(2))

3.14

    • print(lessDecimalPlaces(3)) # 3.142 rather than 3.141 3.142

```

Notations:

        ◦ You should use $\pi$ from the module `math`.

        ◦ The return value of your function should be a 'float' rather than a 'str'.

        ◦ The parameter is less than 15.




    5. Define a class `EquilateralTriangle`(等边三角形), whose attribute is side length. You should provide a function (method) to get its area and its height.

```python

2

# Define a class

tri1 = EquilateralTriangle(2)  # side length is 2

print(tri1.area())

print(tri1.height())

```

    1. Submit your Source Code

Please compress your files including the source code *.py to a package named as ”Student ID_ Name (ProjectNumber_VersionNumber) .rar”, such as 5180309000 _XXX (assignment1 v3) .rar, then send it to your course Teaching assistant. The final version would be checked and scored.


2. The deadline for the first lab assignment is due at 11 pm, on 2019-11-01 (Friday).









































3

More products