|
3 | 3 | ## Section A
|
4 | 4 | # 1. Create two variables that hold the width and height of a rectangle, work out and store the area in a third variable.
|
5 | 5 | # Print out the string: `Rectangle of width <x> and height <y> has an area of <area>`.
|
6 |
| - |
| 6 | +width=6 |
| 7 | +height=7 |
| 8 | +area = withd * height |
| 9 | +print(Rectangle of witdth " +str(width)+ " and height " str(height) + " has an area of " + str(area)" |
7 | 10 |
|
8 | 11 |
|
9 | 12 | # 2. Write code that prints the length of the string, 'python'.
|
10 |
| - |
| 13 | +print(len("python))" |
11 | 14 |
|
12 | 15 |
|
13 | 16 | # 3. Print out the first and third letter of the word 'python'.
|
14 |
| - |
| 17 | +word = 'python' |
| 18 | +print("python"[0]) |
| 19 | +print("python"[2]) |
15 | 20 |
|
16 | 21 |
|
17 | 22 | # 4. Ask the user to enter their name, and print out `Hello, <name>`.
|
| 23 | +name = input("What is your name?") |
| 24 | +print("Hello, " + name) |
18 | 25 |
|
19 | 26 |
|
20 | 27 |
|
21 | 28 | # 5. Ask the user to enter their age, tell them how old they will be in 15 years time.
|
| 29 | +age = int(input("What is your age? ")) |
| 30 | +age_in_15_years = age+ 15 |
| 31 | +print("In 15 years you will be": " + str(age_in_15_years)) |
| 32 | + |
22 | 33 |
|
23 | 34 |
|
24 | 35 |
|
25 | 36 | # 6. Combine the two input statements above and print out the message `Hello, <name>, you are currently <age> years old.
|
26 | 37 | # In 15 years time you will be <age_in_15_years_time>`.
|
27 | 38 |
|
| 39 | +print("Hello " + name + ", you are currently" + str(age) + "years old. In 15 years time you will be " + str(age_in15_years)) |
28 | 40 |
|
29 | 41 |
|
30 | 42 | # 7. Ask the user to enter their hometown, print it out in uppercase letters.
|
| 43 | +hometown = input("What is your hometown? ) |
| 44 | +print(hometown_uppercase()) |
31 | 45 |
|
32 | 46 |
|
33 | 47 |
|
34 | 48 | # 8. Ask the user to enter their favourite colour and find out the length of the colour they input.
|
35 |
| - |
| 49 | +color = input("What is your favorite color? ") |
| 50 | +print(len(colour)) |
36 | 51 |
|
37 | 52 |
|
38 | 53 | # 9. Ask the user to enter the weather and the month. Print out the string, `It is <month> and it is <weather> today`.
|
| 54 | +weather = input("What is the weather today?") |
| 55 | +month = input("What month is it?") |
| 56 | + |
| 57 | +print("It is " + month + " and it is " + weather + " today.") |
39 | 58 |
|
40 | 59 |
|
41 | 60 |
|
42 | 61 | # 10. Ask the user to enter 5 different temperatures and the month. Work out the average temperature and print out the string:
|
43 | 62 | # `It is <month> and the average temperature is <average_temperature> degrees celsius`.
|
| 63 | +temp1 = int(What is the temperature?") |
| 64 | +temp2 = int(What is the temperature?") |
| 65 | +temp3 = int(What is the temperature?") |
| 66 | +temp4 = int(What is the temperature?") |
| 67 | +temp5 = int(What is the temperature?") |
44 | 68 |
|
45 |
| - |
| 69 | +average+(temp1 + temp2 + temp3 + temp4 + temp5) / 5 |
| 70 | +print `It is + month + "and the average temperature is " + str(average)) |
46 | 71 |
|
47 | 72 | # 11. Print out the above sentence but make the month upper case.
|
48 |
| - |
| 73 | +print `It is + month.upper() + "and the average temperature is " + str(average)) |
49 | 74 |
|
50 | 75 |
|
51 | 76 | # 12. Create a variable that holds your favourite animals and print it out.
|
52 | 77 | # Make sure the animals are all on different lines and tabbed.
|
53 |
| - |
| 78 | +favorite_animals = "Elephant\n\tLion\n\t\tDolphin\n\t\t\tPenguin" |
| 79 | +print(favorite_animals) |
54 | 80 |
|
55 | 81 |
|
56 | 82 | # 13. Ask the user to enter their name as well as a number.
|
57 | 83 | # Print out the uppercase character at that position in the string.
|
| 84 | +name = input("Enter your name: ") |
| 85 | +number = int(input("Enter a number: ")) |
| 86 | + |
| 87 | +if number < len(name): |
| 88 | + uppercase_char = name[number].upper() |
| 89 | + print("Uppercase character at position", number, "is:", uppercase_char) |
| 90 | +else: |
| 91 | + print("Invalid position. The name is not long enough.") |
58 | 92 |
|
59 | 93 |
|
60 | 94 |
|
61 | 95 | # 14. Slice the string with steps of 2, excluding the first and last characters of the string "WelcometoPython".
|
| 96 | +strin="WelcometoPython" |
| 97 | +print(len(string)) |
| 98 | +print(string[1:14:2]) |
0 commit comments