8000 finish exercise for session2 · Pcodias/intro-to-python@29aeaac · GitHub
[go: up one dir, main page]

Skip to content

Commit 29aeaac

Browse files
author
Replit user
committed
finish exercise for session2
1 parent b133eff commit 29aeaac

File tree

1 file changed

+44
-7
lines changed

1 file changed

+44
-7
lines changed

session_02/exercises_2.py

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,59 +3,96 @@
33
## Section A
44
# 1. Create two variables that hold the width and height of a rectangle, work out and store the area in a third variable.
55
# 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)"
710

811

912
# 2. Write code that prints the length of the string, 'python'.
10-
13+
print(len("python))"
1114

1215

1316
# 3. Print out the first and third letter of the word 'python'.
14-
17+
word = 'python'
18+
print("python"[0])
19+
print("python"[2])
1520

1621

1722
# 4. Ask the user to enter their name, and print out `Hello, <name>`.
23+
name = input("What is your name?")
24+
print("Hello, " + name)
1825

1926

2027

2128
# 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+
2233

2334

2435

2536
# 6. Combine the two input statements above and print out the message `Hello, <name>, you are currently <age> years old.
2637
# In 15 years time you will be <age_in_15_years_time>`.
2738

39+
print("Hello " + name + ", you are currently" + str(age) + "years old. In 15 years time you will be " + str(age_in15_years))
2840

2941

3042
# 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())
3145

3246

3347

3448
# 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))
3651

3752

3853
# 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.")
3958

4059

4160

4261
# 10. Ask the user to enter 5 different temperatures and the month. Work out the average temperature and print out the string:
4362
# `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?")
4468

45-
69+
average+(temp1 + temp2 + temp3 + temp4 + temp5) / 5
70+
print `It is + month + "and the average temperature is " + str(average))
4671

4772
# 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))
4974

5075

5176
# 12. Create a variable that holds your favourite animals and print it out.
5277
# 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)
5480

5581

5682
# 13. Ask the user to enter their name as well as a number.
5783
# 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.")
5892

5993

6094

6195
# 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

Comments
 (0)
0