Skip to content

Commit bb9ec66

Browse files
committed
Added Section 14
1 parent 99f2a29 commit bb9ec66

15 files changed

+1884
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"python.pythonPath": "C:\\Users\\DELL\\anaconda3\\python.exe"
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
###########################
2+
## PART 10: Simple Game ###
3+
### --- CODEBREAKER --- ###
4+
## --Nope--Close--Match-- ##
5+
###########################
6+
7+
# It's time to actually make a simple command line game so put together everything
8+
# you've learned so far about Python. The game goes like this:
9+
10+
# 1. The computer will think of 3 digit number that has no repeating digits.
11+
# 2. You will then guess a 3 digit number
12+
# 3. The computer will then give back clues, the possible clues are:
13+
#
14+
# Close: You've guessed a correct number but in the wrong position
15+
# Match: You've guessed a correct number in the correct position
16+
# Nope: You haven't guess any of the numbers correctly
17+
#
18+
# 4. Based on these clues you will guess again until you break the code with a
19+
# perfect match!
20+
21+
# There are a few things you will have to discover for yourself for this game!
22+
# Here are some useful hints:
23+
24+
# Try to figure out what this code is doing and how it might be useful to you
25+
import random
26+
digits = list(range(10))
27+
random.shuffle(digits)
28+
print(digits[:3])
29+
30+
# Another hint:
31+
guess = input("What is your guess? ")
32+
print(guess)
33+
34+
# Think about how you will compare the input to the random number, what format
35+
# should they be in? Maybe some sort of sequence? Watch the Lecture video for more hints!
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
###########################
2+
## PART 10: Simple Game ###
3+
### --- CODEBREAKER --- ###
4+
## --Nope--Close--Match--##
5+
###########################
6+
7+
# It's time to actually make a simple command line game so put together everything
8+
# you've learned so far about Python. The game goes like this:
9+
10+
# 1. The computer will think of 3 digit number that has no repeating digits.
11+
# 2. You will then guess a 3 digit number
12+
# 3. The computer will then give back clues, the possible clues are:
13+
#
14+
# Close: You've guessed a correct number but in the wrong position
15+
# Match: You've guessed a correct number in the correct position
16+
# Nope: You haven't guess any of the numbers correctly
17+
#
18+
# 4. Based on these clues you will guess again until you break the code with a
19+
# perfect match, the game will report "CODE CRACKED"!
20+
21+
# There are a few things you will have to discover for yourself for this game!
22+
# Here are some useful hints:
23+
24+
import random
25+
26+
def get_guess():
27+
'''
28+
Asks for the number guess and transforms the string to a list.
29+
'''
30+
return list(input("What is your guess?"))
31+
32+
def generate_code():
33+
'''
34+
generates a 3 digit list for the code
35+
'''
36+
digits = [str(num) for num in range(10)]
37+
random.shuffle(digits)
38+
return digits[:3]
39+
40+
def generate_clues(code,userGuess):
41+
'''
42+
Takes in a user guess and code then compares the numbers in a loop and
43+
creates a list of clues according to the matching parameters.
44+
'''
45+
if userGuess == code:
46+
return "CODE CRACKED"
47+
48+
clues = []
49+
50+
# Compare guess to code
51+
for ind,num in enumerate(userGuess):
52+
if num == code[ind]:
53+
clues.append("Match")
54+
elif num in code:
55+
clues.append("Close")
56+
if clues == []:
57+
return ["Nope"]
58+
else:
59+
return clues
60+
61+
# Run Game
62+
print("Welcome Code Breaker! Let's see if you can guess my 3 digit number!")
63+
64+
# Create a Secret Code to start the Game
65+
secretCode = generate_code()
66+
print("Code has been generated, please guess a 3 digit number")
67+
#print(secretCode)
68+
69+
# Empty Clue Report to Start with
70+
clueReport = []
71+
72+
# Keep asking until the user has gotten it right!
73+
while clueReport != "CODE CRACKED":
74+
75+
# Ask for guess
76+
guess = get_guess()
77+
78+
# Give the clues
79+
clueReport = generate_clues(guess,secretCode)
80+
print("Here is the result of your guess:")
81+
for clue in clueReport:
82+
print(clue)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
#####################################
2+
#### Numbers and more in Python! ####
3+
#####################################
4+
5+
# In this lecture, we will learn about numbers in Python and how to use them.
6+
#
7+
# We'll learn about the following topics:
8+
#
9+
# 1.) Types of Numbers in Python
10+
# 2.) Basic Arithmetic
11+
# 3.) Differences between Python 2 vs 3 in division
12+
# 4.) Object Assignment in Python
13+
14+
# Types of numbers
15+
#
16+
# Python has various "types" of numbers (numeric literals). We'll mainly focus on
17+
# integers and floating point numbers.
18+
#
19+
# Integers are just whole numbers, positive or negative. For example: 2 and -2 are
20+
# examples of integers.
21+
#
22+
# Floating point numbers in Python are notable because they have a decimal point
23+
# in them, or use an exponential (e) to define the number. For example 2.0 and -2.1
24+
# are examples of floating point numbers. 4E2 (4 times 10 to the power of 2) is
25+
# also an example of a floating point number in Python.
26+
#
27+
# Throughout this course we will be mainly working with
28+
# integers or simple float number types.
29+
30+
31+
# Now let's start with some basic arithmetic.
32+
33+
# Basic Arithmetic
34+
# Addition
35+
2+1
36+
37+
# Subtraction
38+
2-1
39+
40+
# Multiplication
41+
2*2
42+
43+
44+
# Division
45+
3/2
46+
47+
# Powers
48+
2**3
49+
50+
# Can also do roots this way
51+
4**0.5
52+
53+
# Order of Operations followed in Python
54+
2 + 10 * 10 + 3
55+
56+
# Can use parenthesis to specify orders
57+
(2+10) * (10+3)
58+
59+
60+
## Variable Assignments
61+
#
62+
# Now that we've seen how to use numbers in Python as a calculator let's see how
63+
# we can assign names and create variables.
64+
#
65+
# We use a single equals sign to assign labels to variables.
66+
# You also don't need to specify the keyword var.
67+
# Let's see a few examples of how we can do this.
68+
69+
# Let's create an object called "a" and assign it the number 5
70+
a = 5
71+
72+
# Now if I call a in my Python script, Python will treat it as the number 5.
73+
74+
# Adding the objects
75+
a+a
76+
77+
# What happens on reassignment? Will Python let us write it over?
78+
79+
# Reassignment
80+
a = 10
81+
82+
# Check
83+
a
84+
85+
86+
# Yes! Python allows you to write over assigned variable names. We can also use
87+
# the variables themselves when doing the reassignment. Here is an example of what I mean:
88+
89+
# Check
90+
a
91+
92+
# Use A to redefine A
93+
a = a + a
94+
95+
# Check
96+
a
97+
98+
99+
# The names you use when creating these labels need to follow a few rules:
100+
#
101+
# 1. Names can not start with a number.
102+
# 2. There can be no spaces in the name, use _ instead.
103+
# 3. Can't use any of these symbols :'",<>/?|\()!@#$%^&*~-+
104+
# 3. It's considered best practice (PEP8) that the names are lowercase.
105+
#
106+
# Using variable names can be a very useful way to keep track of different
107+
# variables in Python. For example:
108+
109+
# Use object names to keep better track of what's going on in your code!
110+
my_income = 100
111+
112+
tax_rate = 0.1
113+
114+
my_taxes = my_income*tax_rate
115+
116+
# Show my taxes!
117+
my_taxes
118+
119+
120+
# So what have we learned? We learned some of the basics of numbers in Python.
121+
# We also learned how to do arithmetic and use Python as a basic calculator.
122+
# We then wrapped it up with learning about Variable Assignment in Python.
123+
#
124+
# Up next we'll learn about Strings!

0 commit comments

Comments
 (0)