Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to take in text input from a keyboard and store it into a variable?

I would just like something simple to read text from a keyboard and store it into a variable. So for:

var color = 'blue'

I would like the user to provide input for the color from the keyboard. Thank you!

like image 755
Zemprof Avatar asked Oct 31 '13 23:10

Zemprof


People also ask

How do you read the input from keyboard and explain with a program?

Python user input from the keyboard can be read using the input() built-in function. The input from the user is read as a string and can be assigned to a variable. After entering the value from the keyboard, we have to press the “Enter” button. Then the input() function reads the value entered by the user.

How do I get keyboard input in python?

Use the input() function to get Python user input from keyboard. Press the enter key after entering the value. The program waits for user input indefinetly, there is no timeout.

What is the command to receive input from the keyboard?

Keyboard Input: scanf()

How do you give a user a choice in Python?

Python User Input Choice Example:value1 = input("Please enter first integer:\n") value2 = input("Please enter second integer:\n") v1 = int(value1) v2 = int(value2) choice = input("Enter 1 for addition. \nEnter 2 for subtraction.


2 Answers

I would suggest the readline-sync module as well if you don't require something asynchronous.

# npm install readline-sync

const readline = require('readline-sync');

let name = readline.question("What is your name?");

console.log("Hi " + name + ", nice to meet you.");
like image 194
Harlin Avatar answered Oct 07 '22 21:10

Harlin


Node has a built in API for this...

const readline = require('readline');

const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

rl.question('Please enter a color? ', (value) => {
    let color = value
    console.log(`You entered ${color}`);
    rl.close();
});
like image 37
Olutobi Adeyemi Avatar answered Oct 07 '22 22:10

Olutobi Adeyemi