Our first script, my_program.py, is far from perfect. As it is, it will always compute the same angle every time it is run! This is not helpful after the first time. Instead, we want to take input from the command line and calculate the angle fresh each time. To do this, we can use Python's input() function.
input() takes a string as a parameter, which it prints to the screen. The function then waits until the user inputs some text (and hits enter), at which point it returns a string. So, if we wrote:
name = input("Enter the participant's name: ")
Then the program will print the string, and wait until something is entered. Once it is entered, input() stores the entered text (as a string) in name.
Great, now we can get text and print it back to the screen (print(name)), but what if we want to get a number as an input? input() always returns a string, so we need to convert it to an int when we get it. Python allows you to convert a different type to an int by using int():
number = int( input('input a number: ') )
This syntax might look strange, but really all that is happening here is we are using the return value of one function as the input to another. To break it down a bit, here is a different way we could have done the same thing:
number_str = input("Input a number: ")
number_int = int(number_string)
Now that you know how to take inputs from a user, let's make our my_program.py script a bit more versatile. See if you can modify my_program.py so that it takes a name, and two sides of a triangle, then prints the correct angle. If you did it right, your output should look something like this:
input() takes a string as a parameter, which it prints to the screen. The function then waits until the user inputs some text (and hits enter), at which point it returns a string. So, if we wrote:
name = input("Enter the participant's name: ")
Then the program will print the string, and wait until something is entered. Once it is entered, input() stores the entered text (as a string) in name.
Great, now we can get text and print it back to the screen (print(name)), but what if we want to get a number as an input? input() always returns a string, so we need to convert it to an int when we get it. Python allows you to convert a different type to an int by using int():
number = int( input('input a number: ') )
This syntax might look strange, but really all that is happening here is we are using the return value of one function as the input to another. To break it down a bit, here is a different way we could have done the same thing:
number_str = input("Input a number: ")
number_int = int(number_string)
Now that you know how to take inputs from a user, let's make our my_program.py script a bit more versatile. See if you can modify my_program.py so that it takes a name, and two sides of a triangle, then prints the correct angle. If you did it right, your output should look something like this:
A couple of tips for print(). Remember you can add multiple parameters to print()! For example,
print(param1, param2, "This is a text string", param3)
The function will insert spaces after each parameter for you. If you want to format a few strings nicely, you can concatenate them by "adding" them together:
print("This is a string " + str + "!")
Concatenating strings will not have spaces inserted between them like separate parameters do.
Finally, you can call print() without any parameters to get just a blank line.
If you are having trouble with this, you can see a solution HERE.
Once you have figured it out, play around with different inputs. Notice that if you provide text when it asks for a number (where we make our int() function call), Python crashes with an error. This may seem frustrating; however, it is very helpful for a program to crash hard earlier rather than later. It helps the program avoid dangerous operations, and potentially illegal (as far as our computer is concerned) actions.
The error outputs will be multiple lines of filenames that tells you exactly which files the error comes from. Finally, the last line will show a line number, filename, and snapshot of the file with the error. Then, Python will describe the error. Sometimes the errors can be confusing, but this one is fairly self-explanatory. If you run into an error that you don't understand, Stack Overflow is always helpful in resolving these issues.
Once your comfortable with user input, on to the next tutorial!
print(param1, param2, "This is a text string", param3)
The function will insert spaces after each parameter for you. If you want to format a few strings nicely, you can concatenate them by "adding" them together:
print("This is a string " + str + "!")
Concatenating strings will not have spaces inserted between them like separate parameters do.
Finally, you can call print() without any parameters to get just a blank line.
If you are having trouble with this, you can see a solution HERE.
Once you have figured it out, play around with different inputs. Notice that if you provide text when it asks for a number (where we make our int() function call), Python crashes with an error. This may seem frustrating; however, it is very helpful for a program to crash hard earlier rather than later. It helps the program avoid dangerous operations, and potentially illegal (as far as our computer is concerned) actions.
The error outputs will be multiple lines of filenames that tells you exactly which files the error comes from. Finally, the last line will show a line number, filename, and snapshot of the file with the error. Then, Python will describe the error. Sometimes the errors can be confusing, but this one is fairly self-explanatory. If you run into an error that you don't understand, Stack Overflow is always helpful in resolving these issues.
Once your comfortable with user input, on to the next tutorial!