1 of 59

code.cs50.io

2 of 59

This is CS50 for MBAs.

3 of 59

4 of 59

code.cs50.io

5 of 59

code hello.py

6 of 59

print("Hello, world.")

7 of 59

int main(void)

{

printf("Hello, world.\n");

}

8 of 59

python hello.py

9 of 59

python hello.py

10 of 59

python hello.py

Interpreter

11 of 59

python hello.py

Interpreter

Python program

12 of 59

compilation

vs.

interpretation

13 of 59

E

01000101

P

01010000

)

00101001

H

01001000

R

01010010

"

00100010

I

01001001

T

01010100

.

00101110

L

01001100

N

01001110

;

00111011

O

01001111

(

00101000

14 of 59

tradeoffs

15 of 59

hello

16 of 59

hello.py, version 2

  • Modify your program in such a way that it instead prompts the user for their name and then prints "hello, so-and-so", where so-and-so is their name.
  • You might find input of help.

17 of 59

vertical

18 of 59

19 of 59

20 of 59

vertical.py, version 1

  • Implement a program that prints 3 hashes (#), one per line.

21 of 59

vertical.py, version 2

  • Modify your program in such a way that it first prompts the user for a (positive) integer and then prints that many hashes (#), one per line.

22 of 59

horizontal

23 of 59

24 of 59

25 of 59

horizontal.py, version 1

  • Implement a program that prints four question marks, followed by a newline.

26 of 59

horizontal.py, version 2

  • Modify your program in such a way that it instead prompts the user for a (positive) integer and then prints that many question marks, followed by a newline.

27 of 59

square

28 of 59

29 of 59

30 of 59

square.py, version 1

  • Implement a program that prints a grid of hashes (#) of height 3 and width 3.

31 of 59

square.py, version 2

  • Modify your program in such a way that it instead prompts the user for a (positive) integer and then prints a grid of hashes (#) of that width and height.

32 of 59

square.py, version 3

  • Adapt your previous solution so that you define a function, square, which takes as input an argument n.
  • Call square to print your square.

33 of 59

square.py, version 4

  • Modify your program in such a way that, if the user doesn't input a positive integer, it prompts them again and again until they do.

34 of 59

mario

35 of 59

36 of 59

mario.py, for those less comfortable

  • Implement a program that prompts the user for a (positive) integer and then prints a right-aligned pyramid of hashes (#) of that height.

37 of 59

38 of 59

mario.py, for those more comfortable

  • Implement a program that prompts the user for a (positive) number and then prints pyramids of hashes (#) of that height.

39 of 59

40 of 59

cash

41 of 59

10¢

25¢

42 of 59

Problem

Given an amount of change, what is the minimum number of coins we can use to make that change?

43 of 59

30¢

44 of 59

30¢

30 coins

45 of 59

30¢

3 coins

46 of 59

30¢

2 coins

47 of 59

47¢

47 cents remaining

0 coins used

48 of 59

47¢

0 cents remaining

5 coins used

49 of 59

Pseudocode

50 of 59

Test Cases

15¢

$1.60

-$1

51 of 59

→ 

→ 1 coin

52 of 59

15¢ → 

→ 2 coins

53 of 59

$1.60 → 

→ 7 coins

54 of 59

-$1 → 

→ Reprompt

55 of 59

Cash

In a file called cash.py, write a program that prompts the user for how much change is owed and then prints the minimum number of coins with which that change can be made.

$ python cash.py

Change owed: 0.28

4

56 of 59

Distribution Code

If feeling less comfortable, in your terminal, type:

wget https://cdn.cs50.net/hbs/2022/spring/labs/1/cash/less/cash.py

followed by Enter.

If feeling more comfortable, in your terminal, type:

wget https://cdn.cs50.net/hbs/2022/spring/labs/1/cash/more/cash.py

followed by Enter.

57 of 59

Extra Challenge

Tolerate a $ character in the user's input

58 of 59

Critique

59 of 59

This was CS50 for MBAs.

hbs@cs50.harvard.edu