text
stringlengths
0
897
{caption:"Incorrect dog-years program"}
```miniscript
age = input("Enter your age: ")
print "That's " + age*7 + " in dog years!"
```
Try that out and see what happens. The program above asks your age, so the user probably enters something like "42", which gets stored in the variable `age`. But it's still a string, so when we do `age*7`, we get "42424242424242" — that is, the string "42" repeated seven times.
The solution is the `val` function, which we briefly mentioned in Chapter 2. This takes a string argument and returns its numeric value. To fix the program above, we just need to use this to convert the user's input to a number. That could be done in several ways; we could do it in the math expression where the numb...
{caption:"Dog-years program, corrected version 1"}
```miniscript
age = input("Enter your age: ")
print "That's " + val(age)*7 + " in dog years!"
```
Or we could do it around the `input`, so we get a number right away...
{caption:"Dog-years program, corrected version 2"}
```miniscript
age = val(input("Enter your age: "))
print "That's " + age*7 + " in dog years!"
```
Or we could even add a new line, between where we get `age` as a string and where we use `age` as a number, that reassigns the variable with the numeric value.
{caption:"Dog-years program, corrected version 3"}
```miniscript
age = input("Enter your age: ")
age = val(age)
print "That's " + age*7 + " in dog years!"
```
All of these work just fine; you should use whichever makes the most sense to you.
Also try entering a string that does *not* look like a number. Enter "fish" for example. What result to you get?
A> If given a string that does not appear to be a number, `val` returns 0.
However, it's smart enough to correctly convert negative numbers, numbers with a decimal point, etc. Experiment with different values until you're comfortable with it. `val` is one of the most common functions you will use in all your MiniScript programming, so it's well worth spending a little time on it.
Try one more example to see how `input`, `val`, and math all work together to do handy calculations. (Remember that the rest of the line after `//` is a comment for the human reader, and ignored by the computer.)
{caption:"Yearly income calculator"}
```miniscript
// Yearly income calculator.
// Enter your income per hour, and hours worked per week,
// and this will calculate your income per month and year.
amountPerHour = val(input("Hourly rate? "))
hoursPerWeek = val(input("Hours per week? "))
weeksPerYear = 50 // assume 2 weeks vacation
amountPerYear = amountPerHour * hoursPerWeek * weeksPerYear
print "That's " + amountPerYear + " per year,"
print "or about " + round(amountPerYear/12) + " per month!"
```
{gap:40}
A> **Chapter Review**
A> - You learned how use `input` to get a string from the user.
A> - You used the `val` function to convert a string to a numeric value.
A> - You've entered programs that can be run again and again, calculating different results based on user inputs.
{chapterHead: "Day 5: Comparisons and Looping", startingPageNum:43}
{width: "50%"}
![](Chapter5.svg)
Q> You might not think that programmers are artists, but programming is an extremely creative profession.
Q> It's logic-based creativity.
Q>— John Romero (co-founder, id Software)
A> **Chapter Objectives**
A> - Learn how to compare two values to see if they are equal, or which one is greater.
A> - Learn to use `while` and `for` to make blocks of code repeat.
A> - Obtain and start relying on the *MiniScript Quick Reference*.
{i:"hard-coded value;value, hard-coded"}
By now, you know several ways to get values into your computer program. You can put them right into the code (these are known as "hard-coded" values); you can ask the user for them with `input`; or you can calculate them from other values. Doing calculations with them is great, but sometimes what you need is to make ...
{i:"comparison operator"}
comparison operator
: a type of operator that compares two values in some way
For example: suppose you wonder whether your computer really understands math at all, so you decide to check whether it knows that 2 plus 2 equals 4. Write:
```miniscript
print 2 + 2 == 4
```
The comparison operator here is `==`, pronounced "equals." Notice that this operator is written with *two* equal signs. A single equal sign is used for assignment, as you learned on Day 3.
D> To test two values for equality, remember to use *two* equal signs: `==`
So in the example above, we are comparing the result of `2 + 2`, on the left side of `==`, to the value `4`, on the right. Run this code and you should see the value `1` in the output. In MiniScript, `1` means "true" and `0` means "false." So MiniScript here is saying that yes, indeed, two plus two is equal to four.