Stanford CS101: Code Variables Lecture 3
Code Variables
In this section, I want to add the idea of variables in code.

Variables in CS101
Variables work as a shorthand -- we = assign a value into a variable, and then use that variable on later lines to retrieve that value. In the simplest case, this just works to avoid repeating a value: we store the value once, and then can use it many times. All computer languages have some form of variable like this -- storing and retrieving values.
Code Example - The Curse
Change the code below so it produces the following output. Use a variable to store the string "Alice" in a variable on the first line like x = "Alice";, then use the variable x on the later lines. In this way, changing just the first line to use the value "Bob" or "Zoe" or whatever changes the output of the whole program.
Alice Alice Alice I had a crush on Alice
x = "Alice";
print(x, x, x);
print("I had a crush on", x);
Next: that's code with variables, now you try it.
Comments
Post a Comment