Skip to content

Latest commit

 

History

History
551 lines (411 loc) · 7.54 KB

week-005.md

File metadata and controls

551 lines (411 loc) · 7.54 KB

[DAY-35] Lists; While; Continue

This week there will be less talking and more programming.

Make a program to help you to decide what to do:

import random

possible = ["nothing"]
while True:
  x = input("What would you like to do: ")
  if x == "done":
    break
  else:
    possible.append(x)

what = random.choice(possible)
print("possible choices: ")
print(possible)
print("the magic 8 ball decided: " + what)

Make a program to help you multiply two numbers:

while True:
  a = int(input("first number: "))
  b = int(input("second number: "))
  print(a, "*", b, "=", a*b)

Here I do a little trick, and make print print numbers instead of making them into string by passing it as separate parameters to the print function. otherwise I have to do:

while True:
  a = int(input("first number: "))
  b = int(input("second number: "))
  print(str(a) + " *" + str(b) + "=" + str(a*b))

What is your spy name?

name = input("what is your name: ")
print("Your spy name is: " + name[0] + name[1])

You can get individual characters from string by picking them up with [] so if name is 'jane' then name[0] is 'j'

Make a calculator

while True:
  a = int(input("first number: "))
  b = int(input("second number: "))
  op = input("operation * / + :")
  result = 0
  if op == "*":
    result = a * b
  elif op == "/":
    result = a/b
  elif op == "+":    
    result = a+b
  else:
    print("I dont understand " + op)
    continue
  print(a,op,b,'=',result)

See how we use continue to not print a result if we don't understand the operation. continue jumps to the beginning of the loop.

[DAY-36] While; Range; Functions

Print the numbers from 0 to 9 FOREVER

while True:
  for i in range(10):
    print(i)

Print each character of a string:

name = input("what is your name: ")
for c in name:
  print(c)

Sum the numbers from 0 to 99

sum = 0

for i in range(100):
  sum = sum + i
print(sum)

Print all the prime numbers from 0 to 100:

def is_prime(n):
  if n == 1 or n == 0:
    return False

  for i in range(2, n):
    if n % i == 0:
      return False

  return True

for i in range(100):
  print(i, is_prime(i))

multiply two numbers:

a = int(input("number 1: "))
b = int(input("number 2: "))
c = a * b

print(c)

Timer:

import time

s = int(input("how many seconds: "))
for i in range(s):
  print(i)
  time.sleep(1)

print(s, "SECONDS ARE OVER")

[DAY-37] While; Functions

Clock

import datetime
import time
import os

while True:
  os.system('clear')
  now = datetime.datetime.now()
  print(now)
  time.sleep(1)

Calendar

def show(calendar):
    for row in calendar:
        for day in row:
            print(day,end=' ')
        print('')

june = [
    ['Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa'],
    ['  ', '  ', ' 1', ' 2', ' 3', ' 4', ' 5'],
    [' 6', ' 7', ' 8', ' 9', '10', '11', '12'],
    ['13', '14', '15', '16', '17', '18', '19'],
    ['20', '21', '22', '23', '24', '25', '26'],
    ['27', '28', '29', '30', '  ', '  ', '  '],
]

show(june)

Growing list of food.

food = []

while True:
  what = input("what is your favorite food: ")
  food.append(what)
  print(what)

Guessing game

colors = ["red","green","blue"]
score = 0
while True:
  guess = input("guess a color: ")
  if guess in colors:
    print("good guess! I like " + guess)
  else:
    print("nop, I dont like " + guess)

Guess again

def is_in_list(list,what):
  for element in list:
    if element == what:
      return True
  return False

colors = ["red","green","blue"]
score = 0
while True:
  guess = input("guess a color: ")
  if is_in_list(colors, guess):
    print("good guess! I like " + guess)
  else:
    print("nop, I dont like " + guess)

[DAY-38] Strings

What was your name again?

for i in range(10):
  name = input("what is your name: ")
  print(i)
  print(name)

First and last letter:

name = input("what is your name: ")
print("first letter: " + name[0])
print("last letter: " + name[len(name)-1])

to get the last letter we have to count however letters are in the whole name, so len(name) will return 4 for 'jane' and it counts from 1

len("jane"):

j a n e
1 2 3 4


len("ja")

j a
1 2

but when we are using [0] to get to specific character from a string or element from a list, it counts from 0, so

j a n e
0 1 2 3

name = "jane"
name[0] is j
name[1] is a
name[2] is n
name[3] is e

len(name) is 4 (because len counts from 1)
so name[len(name) - 1], is name[4 - 1] or name[3] which is the last letter

Make a Line

def line(width, symbol):
    for x in range(width):
        print(symbol,end='')

line(20, '#')
line(30, '*')
line(40, '_')

Make a triangle

def line(width, symbol):
    for x in range(width):
        print(symbol,end='')

for i in range(100):
  line(i,'*')
  print('')

Make a beam

def line(width, symbol):
    for x in range(width):
        print(symbol,end='')

for i in range(50):
  line(i, ' ')
  line(i, '*')
  print('')

Make a box

def line(width, symbol):
    for x in range(width):
        print(symbol,end='')

def box(width,height,symbol):
    for y in range(height):
        line(width,symbol)
        print('')

box(16,20)
box(10,20,'#')
box(15,13,'*')

[DAY-39] Touch Typing

Do some touch typing.

[DAY-40] Functions; Strings; Range

Make inverse beam

def line(width, symbol):
    for x in range(width):
        print(symbol,end='')

count = 50
for i in range(count):
  line(count - i, ' ')
  line(i, '*')
  print('')

Make a tree

def line(width, symbol):
    for x in range(width):
        print(symbol,end='')

count = 50
for i in range(count):
  half = int((count-i)/2)
  line(half , ' ')
  line(i, '*')
  print('')

Inverse

def line(width, symbol):
    for x in range(width):
        print(symbol,end='')

count = 50
for i in range(count):
  line(count-i, '*')
  print('')

Inverse tree

def line(width, symbol):
    for x in range(width):
        print(symbol,end='')

count = 50
for i in range(count):
  half = int(i/2)
  line(half , ' ')
  line(count-i, '*')
  print('')

Art

def line(width, symbol):
    for x in range(width):
        print(symbol,end='')

while True:
  count = 10
  for i in range(count):
    line(count-i, '*')
    print('')

  for i in range(count):
    line(i, '#')
    print('')

Looks so pretty!

#
##
###
####
#####
######
#######
########
#########
**********
*********
********
*******
******
*****
****
***
**
*

#
##
###
####
#####
######
#######
########
#########
**********
*********
********
*******
******
*****
****
***
**
*

[DAY-41] Basics of Basics

More Art

import random

def line(width, symbol):
    for x in range(width):
        print(symbol,end='')


symbols = ['*','#','%','^','&']
while True:
  count = random.randint(5,80)

  symbol = random.choice(symbols)
  for i in range(count):
    line(count-i, symbol)
    print('')

  symbol = random.choice(symbols)
  for i in range(count):
    line(i, symbol)
    print('')

Staircase

def line(width, symbol):
    for x in range(width):
        print(symbol,end='')


count = 100
for i in range(0, count, 5):
  line(i, '*')
  print('')

Range is so cool, and you didnt even notice!

Try this:

for i in range(0, 100, 10):
  print(i)

it will print

0
10
20
30
40
50
60
70
80
90

so every 10th number, now try:

for i in range(0, 100, 5):
  print(i)

and

for i in range(0, 100, 2):
  print(i)

Of course you dont have to start counting from 0:

for i in range(50, 100, 2):
  print(i)

Do some touch typing if there is time left.