Skip to content

Commit bdbdb60

Browse files
Updating code for numbers module
1 parent b6b6c94 commit bdbdb60

8 files changed

Lines changed: 60 additions & 1 deletion

‎numbers/README.md‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# strings
2+
3+
Python can store and manipulate numbers. You can store integers or float (numbers with decimal places)
4+
5+
- [numeric types](https://docs.python.org/3/library/stdtypes.html#numeric-types-int-float-complex)
6+
7+
When naming variables follow the PEP-8 Style Guide for Python Code
8+
- [PEP-8 Style Guide](https://www.python.org/dev/peps/pep-0008/#naming-conventions)
9+

‎numbers/code_challenge.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Ask a user to enter a number
2+
# Ask a user to enter a second number
3+
# Calculate the total of the two numbers added together
4+
# Print 'first number + second number = answer'
5+
# For example if someone enters 4 and 6 the output should read
6+
# 4 + 6 = 10

‎numbers/code_challenge_solution.py‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Ask a user to enter a number
2+
first_number = input('Enter a number: ')
3+
4+
# Ask a user to enter a second number
5+
second_number = input('Enter another number: ')
6+
7+
# Calculate the total of the two numbers added together
8+
answer = float(first_number) + float(second_number)
9+
10+
# Print 'first number + second number = answer'
11+
# For example if someone enters 4 and 6 the output should read
12+
# 4 + 6 = 10
13+
print(first_number + ' + ' + second_number + ' = ' + str(answer))
14+
15+
# If you do not want the decimal places you could round the answer
16+
print(first_number + ' + ' + second_number + ' = ' + str(round(answer)))
Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
11
days_in_feb = 28
2+
3+
# The print function can accept numbers or strings
4+
print(days_in_feb)
5+
6+
# The + operator can either add two numbers or it can concatenate two strings
7+
# it does not know what to do when you pass it one number and one string
8+
# This line of code will cause an error
29
print(days_in_feb + ' days in February')
3-
# print(str(days_in_feb) + ' days in February')
10+
11+
# You need to convert the number to a string to display the value
12+
# This line of code will work
13+
print(str(days_in_feb) + ' days in February')
414

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
first_num = input('Enter first number ')
22
second_num = input('Enter second number ')
3+
# If you have a string variable containing a number
4+
# And you want to treat it as a number
5+
# You must convert it to a numeric datatype
6+
# int() converts a string to an integer e.g. 5, 8, 416, 506
37
print(int(first_num) + int(second_num))
8+
9+
# float() converts a string to a decimal or float number e.g. 3.14159, 89.5, 1.0
410
print(float(first_num) + float(second_num))
511

‎numbers/doing_math.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
# Because the variables are assigned numeric values when created
2+
# Python knows they are numeric variables
13
first_num = 6
24
second_num = 2
5+
6+
# You can peform a variety of math operations on numeric values
37
print('addition')
48
print(first_num + second_num)
59
print('subtraction')
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
# Python has to guess what datatype a variable should be
2+
3+
# since the input function returns a string, the variables it populates
4+
# will hold string values
15
first_num = input('Enter first number ')
26
second_num = input('Enter second number ')
7+
8+
# Because first_num and second_num are string variables the + operator
9+
# concatenates them just like concatenating first_name and last_name
310
print(first_num + second_num)

‎numbers/print_pi.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1+
# You can use variables to store numeric values
12
pi = 3.14159
23
print(pi)

0 commit comments

Comments
 (0)