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!
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.
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.
Keyboard Input: scanf()
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.
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.");
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();
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With