KRIGOLSON TEACHING
  • NEUROSCIENCE
    • NEUROSCIENCE 100 >
      • NEURO 100 INTRODUCTION
      • NEURO 101 ADVANCED
      • NEURO 102 AGING
      • NEURO 103 MEMORY
      • NEURO 104 DECISION MAKING
      • NEURO 105 LEARNING
      • Research Statistics
    • NRSC 500B / MEDS 470
  • Kinesiology
    • EPHE 245
    • EPHE 357
  • STATISTICS
    • BIOMEDICAL STATISTICS
    • MULTIVARIATE STATISTICS >
      • MULTIPLE REGRESSION
    • RESOURCES
    • R TIPS
  • MATLAB
    • THE BASICS >
      • Hello World
      • BASIC MATHEMATICS
      • VARIABLES
      • Matrices
      • Writing Scripts
      • PATHS AND DIRECTORIES
      • USER INPUT
      • FOR LOOPS
      • WHILE LOOPS
      • IF STATEMENTS
      • RANDOM NUMBERS
    • STATISTICS >
      • LOADING DATA
      • DESCRIPTIVE STATISTICS
      • MAKING FUNCTIONS
      • BAR GRAPHS
      • LINE GRAPHS
      • TTESTS
    • EXPERIMENTS: THE BASICS >
      • DRAWING A CIRCLE
      • DRAWING MULTIPLE OBJECTS
      • DRAWING TEXT
      • DRAWING AN IMAGE
      • PLAYING A TONE
      • KEYBOARD INPUT
      • BUILDING A TRIAL
      • BUILDING TRIALS
      • NESTED LOOPS
      • RIGHT OR WRONG
      • SAVING DATA
    • EXPERIMENTS: ADVANCED >
      • STROOP
      • N BACK
      • Oddball
      • Animation
      • VIDEO
    • EEG and ERP Analysis >
      • ERP Analysis
      • FFT Analysis
      • Wavelet Analysis
  • Directed Studies
    • Advanced Topics in Motor Control A
    • Advanced Topics in Motor Control B
    • An Introduction to EEG
    • Advanced EEG and ERP Methods
    • Neural Correlates of Human Reward Processing
    • Independent Research Project
  • RESOURCES
    • EXCEL
    • HOW TO READ A RESEARCH PAPER
    • HOW TO WRITE A RESEARCH PAPER
  • Workshops
    • Iowa State EEG Workshop 2018
  • Python
    • The Basics >
      • Setting Up Python
      • Hello, world!
      • Basic Math & Using Import
      • Variables
      • Matrices
      • Scripts
      • User Input
      • For Loops

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:
Picture
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!
  • NEUROSCIENCE
    • NEUROSCIENCE 100 >
      • NEURO 100 INTRODUCTION
      • NEURO 101 ADVANCED
      • NEURO 102 AGING
      • NEURO 103 MEMORY
      • NEURO 104 DECISION MAKING
      • NEURO 105 LEARNING
      • Research Statistics
    • NRSC 500B / MEDS 470
  • Kinesiology
    • EPHE 245
    • EPHE 357
  • STATISTICS
    • BIOMEDICAL STATISTICS
    • MULTIVARIATE STATISTICS >
      • MULTIPLE REGRESSION
    • RESOURCES
    • R TIPS
  • MATLAB
    • THE BASICS >
      • Hello World
      • BASIC MATHEMATICS
      • VARIABLES
      • Matrices
      • Writing Scripts
      • PATHS AND DIRECTORIES
      • USER INPUT
      • FOR LOOPS
      • WHILE LOOPS
      • IF STATEMENTS
      • RANDOM NUMBERS
    • STATISTICS >
      • LOADING DATA
      • DESCRIPTIVE STATISTICS
      • MAKING FUNCTIONS
      • BAR GRAPHS
      • LINE GRAPHS
      • TTESTS
    • EXPERIMENTS: THE BASICS >
      • DRAWING A CIRCLE
      • DRAWING MULTIPLE OBJECTS
      • DRAWING TEXT
      • DRAWING AN IMAGE
      • PLAYING A TONE
      • KEYBOARD INPUT
      • BUILDING A TRIAL
      • BUILDING TRIALS
      • NESTED LOOPS
      • RIGHT OR WRONG
      • SAVING DATA
    • EXPERIMENTS: ADVANCED >
      • STROOP
      • N BACK
      • Oddball
      • Animation
      • VIDEO
    • EEG and ERP Analysis >
      • ERP Analysis
      • FFT Analysis
      • Wavelet Analysis
  • Directed Studies
    • Advanced Topics in Motor Control A
    • Advanced Topics in Motor Control B
    • An Introduction to EEG
    • Advanced EEG and ERP Methods
    • Neural Correlates of Human Reward Processing
    • Independent Research Project
  • RESOURCES
    • EXCEL
    • HOW TO READ A RESEARCH PAPER
    • HOW TO WRITE A RESEARCH PAPER
  • Workshops
    • Iowa State EEG Workshop 2018
  • Python
    • The Basics >
      • Setting Up Python
      • Hello, world!
      • Basic Math & Using Import
      • Variables
      • Matrices
      • Scripts
      • User Input
      • For Loops