text stringlengths 0 897 |
|---|
However, there are plans for follow-on books that will be focused on different topics: games with various themes, simulations, and more. They will include program listings, and lots of explanation about how to tackle various problems that come up when writing that sort of code. As those books come out, they will be a... |
## Parting Advice |
You are fortunate to have begun your coding journey with MiniScript, a language that is especially simple and easy to learn, while still being powerful enough to create real programs. MiniScript is still a pretty new language, and you will find that many people have not heard of it. Those people are likely to tell yo... |
*This is not true.* |
You already *are* a programmer, and there is no one language that you have to learn. Eventually you will be comfortable in many languages, but there is no rush to get there. The best tool for the job is whichever one lets you accomplish your goals in the simplest, most efficient manner. In many cases, that will be M... |
At the same time, don't be afraid of learning another language when a genuine need arises. Beginners often mistake *syntax* for *programming.* Syntax is the detailed rules about how to declare variables, write loops, create classes, and so on in a particular language. That stuff is relatively easy to pick up in a ne... |
Be patient with yourself as you build these skills. This book is called *Learn to Code in 30 Days*, and you've done that — but it's in the same sense that you could learn to play the piano in 30 days. You know all the basics and you can understand what the experts are doing, but that doesn't mean you can do it yourse... |
Q> A lot of people believe that... people have their gifts and that they should go out and look for what that gift is. That’s what you are spending your teenage and college years sifting through and sampling things and then finding your gift, but what I find with most college students is that they never find their gift... |
Q>— Anders Ericsson, Ph.D. |
Talent is what you have *after* you've put in hours and hours of productive practice. In the case of programming, that means writing code. This chapter was full of suggestions on where to find programming tasks, from tiny to large, as well as where to share your own unique creations. Use them! And if you find them ... |
You will find that this talent you build in programming spills over to other mental activities, too. Computers, as you know by now, are patient, but require precision. You have to say exactly what you mean; fuzzy thinking doesn't work. So over time you learn to think clearly and precisely, and this will serve you we... |
So have fun! Remember "code a little, test a little." Don't try to do too much at once. And when you do get stuck — as we all do from time to time — don't be shy about asking for help. Every expert was a beginner at some point, and someday *you* will be the expert helping someone who is new. |
You are now part of a web of experience passed from person to person, which stretches back to our prehistoric ancestors, and forward to who-knows-where — the stars, perhaps. With diligence, a good attitude, and a supportive community, there is nothing you can't accomplish. |
Happy coding! |
{gap:100} |
{width:"25%"} |
 |
{chapterHead: "Day 4: Input and Val", startingPageNum:37} |
{width: "50%"} |
 |
Q> I view computer science as a liberal art. It should be something that everybody learns. |
Q>— Steve Jobs (co-founder, Apple Computer) |
A> **Chapter Objectives** |
A> - Learn to use `input` to get a string from the user. |
A> - Use the `val` function to convert a string to a number. |
## Getting input from the user |
In the first three days of this course, you've already learned how to store values in variables, do math with them, and print them out. But those values are always set right in the program code, so your program does the same thing every time. The only exception to that is code that uses the `rnd` function, but that o... |
The answer is the `input` function. This is a built-in function in most MiniScript implementations. |
{i:"`input`"} |
{caption:"`input` intrinsic function"} |
| `input(s="")` | displays prompt s, then returns string entered by the user | |
{i:"prompt"} |
prompt |
: a string displayed to the user when the computer is waiting for input, giving them some idea of what sort of input is needed |
Here's a simple example. |
```miniscript |
name = input("What is your name?") |
print "Hello, " + name + "!" |
``` |
Go ahead and try this on the MiniScript *Try-It!* page. When you run it, you'll see the prompt — "What is your name?" — displayed in the output area, followed by a rectangular blinking cursor. That blinking cursor is your invitation to type something! Go ahead and type your name. (Or the name of your favorite celeb... |
{i:"Return key;Enter key"} |
When you've typed something, press the Return or Enter key, which is your signal to the computer that you're done with your answer. The program then continues with line 2, printing out a personalized greeting. Now run it again, and type something else. You get a different output. |
D> Pause for a moment and think about how cool that is. You can now make programs that can ask for information from the user, process that information, and produce a unique result. Every run of the program can do something new and useful based on the information the user provides. This is the power that got us to th... |
The ability to get input from the user lets you make considerably more chatty programs than you could before. Let's illustrate that with a somewhat longer example. (Remember, the fastest way to learn is to actually type these examples in and run them!) |
{caption:"Cookbook suggestion program"} |
```miniscript |
name = input("Enter your name: ") |
print "Hello, " + name + "." |
food = input("What's your favorite food? ") |
print "You like " + food + "? I can't stand the stuff." |
print "So what region do you live in?" |
region = input("Northern, Eastern, etc.? ") |
print "Well, maybe you should make a cookbook:" |
print name + "'s " + region + " " + food + "!" |
``` |
This program uses three variables: `name`, `food`, and `region`. Each of them is assigned a value from a separate call to the `input` function. You can see that the computer works through this program, step by step, but it pauses on each `input` call, waiting for the user to type something and press return. Then it ... |
{width: "75%"} |
 |
## Getting a *number* from the user |
Recall the difference between strings and numbers, which we covered in Chapter 1. A string is a sequence of characters, like `"Mary"`, while a number is a numeric value, like `42`. Even if the sequence of characters in a string happens to look like a number to us, like `"007"`, it's still just a bunch of characters t... |
The `input` function always returns a string. This is important. |
A> The `input` function always returns a string! |
That's important to remember, because the math operators do different things with strings than they do with numbers. With numbers, `*` is multiplication. But with strings, it is string replication (i.e. repeating). So if you try to do something like the following, it doesn't work as intended. |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.