Skip to content

Solved example for Index-game #39

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 25 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/Khan Sole Academy/credit.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This solved example was written by Mbaoma Chioma Mary (Section Leader of Section 223)
66 changes: 66 additions & 0 deletions examples/Khan Sole Academy/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Khan Sole Academy</title>
</head>
<body>
Now that you’ve seen how programming can help us in a number of different areas, it’s
time for you to implement Khan-sole Academy—a program that helps other people learn!
In this problem, you’ll write a program in the file <b> khansole_academy.py </b> that randomly
generates simple addition problems for the user, reads in the answer from the user, and then
checks to see if they got it right or wrong, until the user appears to have mastered the
material. Note that “console” is another name for “terminal” :-). <br>
More specifically, your program should be able to generate simple addition problems that
involve adding two 2-digit integers (i.e., the numbers 10 through 99). The user should be
asked for an answer to each problem. Your program should determine if the answer was
correct or not, and give the user an appropriate message to let them know. Your program
should keep giving the user problems until the user has gotten 3 problems <b><u>correct in a row.</u></b>
<br>
(Note: the number of problems the user needs to get correctly in a row to complete the
program is just one example of a good place to specify a constant in your program).<br>
A sample run of the program is shown below (user input is in <b> <i> italics</i></b>.)
<br>
<ul>
<li> What is 51 + 79? <br>
Your answer: <i> 120 </i> <br>
Incorrect. The expected answer is 130 </li> <br> <br>

<li>What is 33 + 19? <br>
Your answer: <i> 42 </i> <br>
Incorrect. The expected answer is 52 </li> <br><br>

<li> What is 55 + 11? <br>
Your answer: <i> 66 </i> <br>
Correct! You've gotten 1 correct in a row.</li> <br><br>

<li> What is 84 + 25? <br>
Your answer: <i> 109 </i> <br>
Correct! You've gotten 2 correct in a row.</li> <br> <br>

<li> What is 26 + 58? <br>
Your answer: <i> 74 </i> <br>
Incorrect. The expected answer is 84 </li> <br> <br>

<li> What is 98 + 85? <br>
Your answer: <i> 183 </i> <br>
Correct! You've gotten 1 correct in a row.</li> <br> <br>

<li> What is 79 + 66? <br>
Your answer: <i> 145 </i> <br>
Correct! You've gotten 2 correct in a row </li> <br> <br>

<li> What is 97 + 20? <br>
Your answer: <i> 117 </i> <br>
Correct! You've gotten 3 correct in a row </li> <br>
</ul>
Congratulations! You mastered addition. <br>
As a side note, one of the earliest programs Mehran wrote (with his friend Matthew) when
he was first learning how to program was very similar to Khan-Sole Academy and it was
called “M&M’s Math Puzzles”. It was written in a language named BASIC, on a computer
that had 4K of memory (that’s 4 Kilobytes) and used cassette tapes (the same kind used for
music in the 1970’s) to store information. Yeah, Mehran is old.


</body>
</html>
59 changes: 59 additions & 0 deletions examples/Khan Sole Academy/soln.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# a python program that randomly generates simple addition problems for the user,
# reads in the answer from the user,
# and then checks to see if they got it right or wrong,
# until the user appears to have mastered the material.

#import the random library
import random

#define your function
def main():

#we set the number of iterations to 0 as our starting value
iteration = 0

#we use a while loop so the keeps running as long as the user defined conditions are true
while True:

#since we want the program to end after 3 correct consecutive answers,
#we state this condition in an 'if' statement
if iteration == 3:
print('Congratulations! You mastered addition.')

#'break' keyword breaks out of the loop (ends the program)
break

#if the 'if' block above is wrong, the 'else' code block would run
else:
num_one = random.randint(10, 99)
num_two = random.randint(10, 99)
result = num_one + num_two

#we use the f formatting to set values of the variable names in curly brackets at
#the appropriate positions
print(f'What is {num_one} + {num_two}?')

#we enable the user type in their answers and we convert it to a float using the
#'float' keyword
answer = float(input('Your answer: '))

#we define conditions to test the correctness of the user's input (answer)
if answer == result:

#we increase the value of our iteration (we increase the starting value by 1)
iteration += 1

#the use of '/' is called escaping. It is used to inform the interpreter that,
#the character is part of a string
print(f'Correct! You\'ve gotten {iteration} correct in a row.')

else:
print(f'Incorrect. The expected answer is {result}.')

#the interation value is reduced by one, so the iteration starts counting all over from 1,
#when the if block above is true
iteration -= iteration

#call your function
if __name__ == '__main__':
main()
1 change: 1 addition & 0 deletions examples/Khan Sole Academy/title.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The title of this solved example is Khan Sole Academy
1 change: 1 addition & 0 deletions examples/index-game/credit.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The example was solved by Mbaoma Chioma Mary (Section Leader for Section 223)
65 changes: 65 additions & 0 deletions examples/index-game/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>The Index Game </title>
<style>
table, td {
border: 1px solid black;
border-collapse: collapse;
}
td {
padding: 1px;
}
</style>
</head>
<body>
We are going to take the first step into data science. You will learn about lists and files! <br>
<h3> 1. The Index Game </h3>
As a warmup, read this code and play the game a few times. Use this mental model of the list: <br>
<table style="width: 40%">
<tr>
<th colspan = '5'> Value </th>
<td> 'Julie' </td>
<td> 'Mehran' </td>
<td> 'Simba' </td>
<td> 'Ayesha' </td>
<td> 'Karel' </td>
</tr> <br>
<tr>
<th colspan = '5'> Index </th>
<td> 0 </td>
<td> 1 </td>
<td> 2 </td>
<td> 3 </td>
<td> 4 </td>
</tr> <br>
</table> <br>

<font color="blue"><p style = "margin-left: 40px"> def main(): </font><br>
<p style = "margin-left: 60px"><font color="red"> # 1. Understand how to create a list and add values </font><br>
<font color="red"> # A list is an ordered collection of values </font><br>
names = [<font color="blue">'Julie', 'Mehran', 'Simba', 'Ayesha'] </font><br>
names.append(<font color="blue"> 'Karel' </font>) <br>
<font color="red"> # 2. Understand how to loop over a list </font><br>
<font color="red"> # this prints the list to the screen one value at a time </font><br>
<font color="blue">for </font> value <font color="blue">in </font> names: <br>
print(value) <br>
<font color="red"> # 3. Understand how to look up the length of a list </font><br>
<font color="red"> # use randint to select a valid "index" </font><br>
max_index = len(names) - 1 <br>
index = random.randint(0, max_index) <br>
<font color="red"> # 4. Understand how to get a value by its index </font><br>
<font color="red"> # get the item at the chosen index </font><br>
correct_answer = names[index] <br>
<font color="red"> # This is just like in Khansole Academy... </font><br>
<font color="red"> # prompt the user for an answer, check if it is correct </font><br>
prompt = <font color="blue">'who is in index...' </font> + str(index)+ <font color="blue"> '? ' </font><br>
answer = input(prompt) <br>
<font color="blue"> if </font> answer == correct_answer:<br>
print(<font color="blue">'Good job'</font>)<br>
else:<br>
print(<font color="blue">'Correct answer was' </font>, correct_answer) </p><br>

</body>
</html>
48 changes: 48 additions & 0 deletions examples/index-game/soln.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import random

def main():
#we set up a list of items
names = ['Julie', 'Mehran', 'Simba', 'Ayesha', 'Karel']

#.append is used to add an item to the end of a list
names.append('Omar')

#we use a for loop to iterate through the list and print out individual members of the list
for name in names:
print(name)

#len is used to get the length of a list
#i.e number of items in the list
length_of_list = len(names)

#index of a list starts its count from 0
max_index = length_of_list - 1

#f formats are used to print out strings
#te=he value in brace brakets can be any data type
print(f' the list has {length_of_list} items')
print(f'the list has a maximum index of {max_index}')

#we set the variable name 'index' to a random value provided the value is a valid index
index = random.randint(0,max_index)

#item_index prints the item at the random index above
item_index = names[index]
print(f'a random index is {index}')
print(f'an item at a randomly selected index is {item_index}')

#who is in what index
print(names)
prompt = input(f'Who is at index {index}?: ')
correct_answer = names[index]

#it is possible to assign two or more variable names to two or more values
#the .lower is used to set the values of prompt and correct_answer to lowercase to ensure that
#no matter the format the user enters the values, the interpreter converts it to lowercase
prompt , correct_answer = prompt.lower() , correct_answer.lower()
if prompt == correct_answer:
print('Correct!')
else:
print(f'Correct answer is, {correct_answer}')
if __name__ == '__main__':
main()
1 change: 1 addition & 0 deletions examples/index-game/title.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The title of this solved example is Index Game (Python solution)
1 change: 1 addition & 0 deletions examples/liftoff/credit.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This solved example was written by Mbaoma Chioma Mary (Section Leader of Section 223)
23 changes: 23 additions & 0 deletions examples/liftoff/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Liftoff</title>
</head>
<body>
Write a program in the file liftoff.py that prints out the calls for a spaceship that is
about to launch. Countdown the numbers from 10 to 1 and then write “Liftoff!” Your
program should include a for loop using range. A sample run of the program is below. <br>
10 <br>
9 <br>
8 <br>
7 <br>
6 <br>
5 <br>
4 <br>
3 <br>
2 <br>
1 <br>
Liftoff!
</body>
</html>
26 changes: 26 additions & 0 deletions examples/liftoff/soln.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#a program that works like a countdown call
#the countdown runs from 10 -1 and then prints 'Liftoff'

#define your function
def main():

#we use a for loop and the range funstion which has this format
#range(start, stop, step)
#start - starting value
#stop - stopping value
#step - format of printing the generated numbers
#we set the step value to a negative index so the countdown starts printing from the last
#number in the range (10) and stops at the first number (1)
for num in range(10,0,-1):

#we print out the 'num' values in the range
print(num)

#we print 'LIFTOFF!' outside of the loop so it appears at the end of the countdown and not
#inside the countdown sequence
print('Liftoff!')

#call your function
if __name__ == '__main__':
main()

1 change: 1 addition & 0 deletions examples/liftoff/title.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The title of this solved example is Liftoff and it falls under the Sandcastles problem (Python solution)
1 change: 1 addition & 0 deletions examples/random_numbers/credit.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This solved example was written by Mbaoma Chioma Mary (Section Leader of Section 223)
28 changes: 28 additions & 0 deletions examples/random_numbers/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Random numbers</title>
</head>
<body>
Write a program in the file random_numbers.py that prints 10 random integers (each
random integer should have a value between 0 and 100, inclusive). Your program
should use a constant named NUM_RANDOM, which determines the number of random
numbers to print (with a value of 10). It should also use constants named MIN_RANDOM
and MAX_RANDOM to determine the minimal and maximal values of the random numbers
generated (with respective values 0 and 100). To generate random numbers, you should
use the function random.randint() from Python’s random library. <br>
Example: <br>
35 <br>
10 <br>
45 <br>
59 <br>
45 <br>
100 <br>
8 <br>
31 <br>
48 <br>
6 <br>
It's alright if your code does not generate the exact numbers above.
</body>
</html>
30 changes: 30 additions & 0 deletions examples/random_numbers/soln.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#we want to print 10 random numbers within the range of 0 and 100 (100 inclusive)
#we import the random library because we are printing a list of RANDOM numbers
import random

#define your function
def main():
#set the length of our proposed list of numbers
num_random = 10

#we set our starting value
min_random = 0

#we set our stopping value
max_random = 100

#we use a 'for' loop becuase we know the range/size of our list (i.e. 10)
#range is an inbuilt function in python that is used to specify the length of a list
#or number of iterations a funtion should undertake
for i in range(num_random):

#the variable name 'random_num' contains the random integer to be generated by our script
#random.randint has this format random.randint(start, end)
random_num = random.randint(min_random, max_random)

#we get a list of 10 numbers because we combined the random.randint function with a for loop
print(random_num)

#call the function
if __name__ == '__main__':
main()
1 change: 1 addition & 0 deletions examples/random_numbers/title.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The title of this solved example is Random numbers and it falls under the Sandcastles problem.
1 change: 1 addition & 0 deletions examples/stroop/credit.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This worked example was solved by Mbaoma Chioma Mary, section 223 leader
Loading