1. Home
2. Training Library
3. Data Types and Variables |SDTL3 RS A3.1|

# Input and output a first program

## Contents

###### Input, output and naming variables
Input and output a first program
Overview
Difficulty
Intermediate
Duration
43m
Students
8
Description

In this section, you will explore data types, variables, input and output functions, and discover more about calculations and mathematical operators in Python.

Transcript

Sarah: In this video, we're going to do a slightly more sophisticated programme which takes its input from outside rather than having the input typed into the programme. We're going to allow a user to type in some data that we're then going to do a calculation with. And the problem we're going to solve is we're going to ask them type in some miles, a distance in miles, and we're going to tell them what the distance is in kilometres. So, first of all, I start by explaining what the programme is about, and this time I'm going to do it with a multiline string rather than with a comment. And there is a reason, which we will discuss later on why you might do that. So, we're going to have a multiline string which is explaining the programme, and it is, 'Input a distance in miles and print what it is in kilometres.' Multiline strings are these triple quotes. The input we already have written there, the input is going to be the miles, the output is going to be the kilometres, and the calculation is going be whatever we have to do to change miles into kilometres. So, first of all, you let somebody type in keyboard input in Python using the input built-in function which is spelt, 'Input,' and you can give it a prompt, so they know what they can type. So, I'm going to say, 'Please type a distance in miles.' That's okay.

Now, when they type it I need to put it in a variable, so I can do something with it. I'm going to call my variable, 'Miles,' that will help me remember what it was. So, I'm saying, 'Input. Please type a distance in miles.' I'm going to have to move this along a bit, no worries. If I just ran that so far, I'm going to put a comment saying, 'This is my input.' Then I'm going to have a, 'Calculation.' Then I'm going to have some, 'Output.' These are comments so Python's going to completely ignore them, the ones after the hashes. So, I'm going to then multiply the factor multiplied by I think it's 1.609. You'd probably look it up if you were actually doing this seriously. And then that should change my miles into kilometres, and then I'm going to output it. There is a problem we get with input, and that is that input always gives you a string, a string variable. So, if we're thinking about data types, the data type miles is going to be to start with is a string. So, if I just went on happily and I said, 'Kilometres equals miles times,' asterisk in Python, '1.609,' and then we try running that to see if it's okay so far. 'Run,' 'Run module.' Got to save it. I'll save it as, 'M2KM,' replace it if it exists already. So, I've run it. It's now saying, 'Please type a distance in miles,' which is what I asked it to say. So, that's fine.

And I'm going to type one, five. And then we get an error, and the error is because it says the line it doesn't like is here, 'KM equals miles times 1.609,' and the error is called a, 'Type error.' And it says it, 'Can't multiply a sequence by a non-int of type float.' It's trying to multiply these miles by 1.609, but miles is actually a string variable. That's the problem it's got. So, we've got to actually do something about that. And you're going to have to do this quite often because input always gets a string. But, I did type a number, a five. So, it's thinking of a five just like the writing of a five not a number of a five, and to make that go right I have to change it. I shall do, 'Convert, string input to a number.' I need to do that to actually be able to do any calculations. So, I'm going to say, 'Miles equals-,' I spelt it right, good, 'Miles equals-,' I'm going to use a float. I just use the type, 'Float,' but I use it like a function to say I'm going to take what's in here and I'm going to change it into a float like that. I'm using, 'Float,' because they might give us 6.5 miles. They might not give us a whole number. It seems reasonable. I could have done that with, 'Int,' as well. Then it should be happy to multiply it by 1.609, because at this point if that doesn't fail in any way I've then changed it into a number.

So, let's run that and see if that works. 'Type a distance in miles.' Let's type, '100.' So, it didn't complain, didn't fall over, but it didn't print because I haven't asked it to print, but it looks like it's getting that far now. So, the last thing is to print it out. We've seen print a few times already, and we can give print any amount of things with commas between it, and it will print them all. So, I'm going to do a nice output. I'm going to say, 'Miles,' is my variable, 'Comma, miles is, comma,' now what's my other variable? 'Kilometres, commas, kilometres,' I'll just say, 'KM,' can just say, 'KM.' So, what I'm going to do is I'm going to print the variable miles here that I've got from the input. I'm going to print a string that says, 'Miles is.' Unchanged, that'll print. Then I'm going to print this new variable I've got called, 'Kilometres.' And then I'm going to put, 'KM.' So, if I run that, hopefully it's going to work and I haven't made any mistakes. You always wonder. I'm going to say, 'Ten miles,' here. And it says, 'Ten miles is 16.09 kilometres.' So, that seems to be working fine. And what you would normally be thinking about is, 'What could go wrong? Can I actually put this up on my website and not make my company look stupid? Is this actually going to work properly?' It seems to be working when we run it, we, we give it a number.

Let's just run it again and be sure about that. 'Distance in miles, 66.78.' It seems to think it knows what that is. To test the programme, you would give it some real numbers where you knew the answers, and check that the answers are what they ought to be. You'd also give it some unexpected data where it-, which could happen to it, and it might-, you don't want it to do anything stupid. So, I could also say, if I put a word instead because I'm not paying attention, which you can never trust a user not to be doing something stupid. Known fact. And then it's again got an error, and it's got an error here which is better than our last error. It says this is the line it didn't like now, 'Miles equals float of miles.' It couldn't convert a string to a float, because the string was, 'Yes.' That really makes normal good sense. It can't do it. So, it doesn't cope with having bad data typed in, but it does get the right answer when it's got the right data typed in. We can't sort that out now, but we will be able to sort it out soon. So, what we've done in this video is we've written a short programme that took some input, did a calculation, and gave us an output.