Skip to content

Commit 6c16e45

Browse files
committed
Step 1: Why we use types
1 parent d3857c2 commit 6c16e45

1 file changed

Lines changed: 50 additions & 0 deletions

File tree

‎sprint-5-prep/1-use-types.py‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Exercise-1
2+
3+
# Predict what double("22") will do.
4+
# Then run the code and check. Did it do what you expected?
5+
# Why did it return the value it did?
6+
7+
def double(value):
8+
return value * 2
9+
10+
#Answer
11+
#I guess it'll return 2222. That's because any value in quotes is a string,
12+
# and when strings are multiplied, they repeat the characters.
13+
14+
def double(value):
15+
return value * 2
16+
17+
print("double is:", double("22")) #double is: 2222
18+
19+
# Yes, It returned 2222 as I predicted.
20+
# Python's operator "*" when used with a string and a number, repeats this string as "number" times,
21+
# and it didn't convert the string "22" into number 22.
22+
23+
24+
#Exercise-2
25+
26+
def double(number):
27+
return number * 3
28+
29+
print(double(10))
30+
31+
32+
#Read the above code and write down what the bug is. How would you fix it?
33+
34+
# Answer
35+
# There are two way to fix it:
36+
# 1. Change *3 to *2.
37+
38+
def double(number):
39+
return number * 2
40+
41+
print(double(10))
42+
43+
# or
44+
45+
# 2. Rename the function to "triple" if we want multiply by 3 - it keeps the logic correct.
46+
47+
def triple(number):
48+
return number * 3
49+
50+
print(triple(10))

0 commit comments

Comments
 (0)