User input from keyboard
Obtaining User Input from the Keyboard
Almost every useful program needs to accept data from the person running it. Keyboard input allows a user to type a value that the program stores in a variable and then works with. Without input, programs can only use hard-coded values - they cannot respond to different data or situations.
In all three languages, reading keyboard input always produces a String. If you need a number, you must convert (also called cast) the input to the correct data type straight away. Forgetting to convert is one of the most common errors when writing programs that process numbers.
| Language | Read input (String) | Convert to Integer | Convert to Real |
|---|---|---|---|
| Python | input("prompt") | int(input(...)) | float(input(...)) |
| VB.NET | Console.ReadLine() | CInt(Console.ReadLine()) | CDbl(Console.ReadLine()) |
| C# | Console.ReadLine() | int.Parse(Console.ReadLine()) | double.Parse(Console.ReadLine()) |
Worked Examples
Select an input type to see how to read and use it in each language.
When the expected input is text - a name, a word, a sentence - no conversion is needed. The value read from the keyboard is already a String and can be stored and used directly.
AQA pseudocode:
OUTPUT "What is your name? "
INPUT name
OUTPUT "Hello, " + name + "!"
# Python -- input() always returns a String
name = input("What is your name? ") # no conversion needed
print("Hello, " + name + "!")
' VB.NET -- Console.ReadLine() always returns a String
Console.Write("What is your name? ")
Dim name As String = Console.ReadLine() ' no conversion needed
Console.WriteLine("Hello, " & name & "!")
// C# -- Console.ReadLine() always returns a string
Console.Write("What is your name? ");
string name = Console.ReadLine(); // no conversion needed
Console.WriteLine("Hello, " + name + "!");
When the expected input is a whole number, the String from the keyboard must be converted to an Integer before arithmetic can be performed. Without conversion, the program would treat the number as text and produce incorrect results or crash.
AQA pseudocode:
OUTPUT "Enter your age: "
INPUT age
OUTPUT "In 10 years you will be " + (age + 10)
# Python -- int() converts the String input to an Integer
age = int(input("Enter your age: ")) # conversion happens immediately
print("In 10 years you will be", age + 10)
' VB.NET -- CInt() converts the String to an Integer
Console.Write("Enter your age: ")
Dim age As Integer = CInt(Console.ReadLine()) ' convert immediately
Console.WriteLine("In 10 years you will be " & (age + 10))
// C# -- int.Parse() converts the string to an int
Console.Write("Enter your age: ");
int age = int.Parse(Console.ReadLine()); // convert immediately
Console.WriteLine("In 10 years you will be " + (age + 10));
When a decimal value is needed - a price, a measurement, a percentage - the input must be converted to a real number (called float in Python and double in VB.NET and C#). The conversion function changes the text "4.99" into the number 4.99 so arithmetic works correctly.
AQA pseudocode:
OUTPUT "Enter the price (£): "
INPUT price
OUTPUT "Price with VAT: £" + (price * 1.2)
# Python -- float() converts the String input to a real number
price = float(input("Enter the price (£): ")) # e.g. user types 4.99
print("Price with VAT: £" + str(round(price * 1.2, 2)))
' VB.NET -- CDbl() converts the String to a Double (real number)
Console.Write("Enter the price (£): ")
Dim price As Double = CDbl(Console.ReadLine()) ' e.g. user types 4.99
Console.WriteLine("Price with VAT: £" & Math.Round(price * 1.2, 2))
// C# -- double.Parse() converts the string to a double (real number)
Console.Write("Enter the price (£): ");
double price = double.Parse(Console.ReadLine()); // e.g. user types 4.99
Console.WriteLine("Price with VAT: £" + Math.Round(price * 1.2, 2));
Key Takeaways
- Keyboard input is read using
input()in Python andConsole.ReadLine()in VB.NET and C#. - All three functions always return a String - the program must convert the value to the correct type before using it in arithmetic.
- To read an integer: wrap with
int()in Python,CInt()in VB.NET, orint.Parse()in C#. - To read a real number: wrap with
float()in Python,CDbl()in VB.NET, ordouble.Parse()in C#. - Prompts should be clear and descriptive so the user knows exactly what to type.