Learning Python: Input and Converters

Table of contents

Introduction

The input() function is a fundamental part of Python, allowing developers to capture data from users. However, since input() returns all data as strings, Python provides several built-in functions to convert this input into other data types, such as integers and floats. In this article, we will explore how to use the input() function and how to convert user input into different data types using converters like int() and float(). We will also delve into more complex, real-life examples.

Using the input() Function

The input() function is used to take user input. It reads a line from the input (usually from the user), converts it to a string, and returns this string. The function can optionally display a prompt to the user.

Syntax

input(prompt)

Example

name = input("Enter your name: ")
print(name)

In this example, the program will display “Enter your name:” and wait for the user to enter their name. The entered name is stored in the name variable as a string.

Converting User Input

Since all input from input() is returned as a string, you often need to convert it to another data type. Python provides functions like int() and float() for this purpose.

Converting to Integer

To convert user input to an integer, you use the int() function. This is useful when you need to perform arithmetic operations on the input.

Syntax

int(input(prompt))

Example

num = int(input("Enter a number: "))
print(f"The integer number is: {num}")

In this example, the user is prompted to enter a number. The input is converted to an integer using int() and stored in the num variable.

Converting to Float

Similarly, to convert user input to a floating-point number, you use the float() function. This is useful for inputs that may contain decimal points.

Syntax

float(input(prompt))

Example

float_num = float(input("Enter a decimal number: "))
print(f"The floating number is: {float_num}")

Here, the user is prompted to enter a decimal number. The input is converted to a float using float() and stored in the float_num variable.

Handling Errors

When converting user input, it’s crucial to handle potential errors. For instance, if a user enters a non-numeric value when prompted for a number, Python will raise a ValueError. You can use exception handling (try-except blocks) to manage such situations gracefully.

Example with Error Handling

while True:
    try:
        num = int(input("Enter a number: "))
        print(f"The integer number is: {num}")
        break
    except ValueError:
        print("Invalid input. Please enter a number.")

In this example, the program will keep asking for input until a valid integer is entered.

Real-Life Examples

1. Simple Calculator

A simple calculator can take two numbers and an operation as input from the user and perform the specified operation.

def calculator():
    num1 = float(input("Enter the first number: "))
    operation = input("Enter the operation (+, -, *, /): ")
    num2 = float(input("Enter the second number: "))

    if operation == "+":
        print(f"{num1} + {num2} = {num1 + num2}")
    elif operation == "-":
        print(f"{num1} - {num2} = {num1 - num2}")
    elif operation == "*":
        print(f"{num1} * {num2} = {num1 * num2}")
    elif operation == "/":
        if num2 != 0:
            print(f"{num1} / {num2} = {num1 / num2}")
        else:
            print("Cannot divide by zero.")
    else:
        print("Invalid operation.")

calculator()

2. Guess the Number Game with Multiple Chances

This game generates a random number and allows the user to guess it with multiple attempts.

import random

def guess_the_number():
    number_to_guess = random.randint(1, 10)
    attempts = 0
    max_attempts = 5

    while attempts < max_attempts:
        try:
            user_guess = int(input(f"Guess a number between 1 and 10 (Attempt {attempts+1}/{max_attempts}): "))
            attempts += 1
            if user_guess == number_to_guess:
                print(f"Congratulations! You found the number in {attempts} attempts.")
                break
            elif user_guess < number_to_guess:
                print("Too low!")
            else:
                print("Too high!")
        except ValueError:
            print("Invalid input. Please enter a number.")
    else:
        print(f"Sorry, you didn't guess the number. It was {number_to_guess}.")

guess_the_number()

3. Student Grade Calculator

This program calculates the average grade of a student based on input scores.

def calculate_average_grade():
    scores = []
    num_scores = int(input("Enter the number of scores: "))

    for i in range(num_scores):
        while True:
            try:
                score = float(input(f"Enter score {i+1}: "))
                if 0 <= score <= 100:
                    scores.append(score)
                    break
                else:
                    print("Score must be between 0 and 100.")
            except ValueError:
                print("Invalid input. Please enter a number.")

    average_grade = sum(scores) / num_scores
    print(f"The average grade is: {average_grade:.2f}")

calculate_average_grade()

4. Bank Account Management

This example simulates basic bank account operations like depositing and withdrawing money.

class BankAccount:
    def __init__(self, balance=0):
        self.balance = balance

    def deposit(self):
        amount = float(input("Enter the amount to deposit: "))
        self.balance += amount
        print(f"Deposited ${amount:.2f}. Current balance: ${self.balance:.2f}")

    def withdraw(self):
        amount = float(input("Enter the amount to withdraw: "))
        if amount <= self.balance:
            self.balance -= amount
            print(f"Withdrew ${amount:.2f}. Current balance: ${self.balance:.2f}")
        else:
            print("Insufficient funds.")

def main():
    account = BankAccount()
    while True:
        print("\n1. Deposit\n2. Withdraw\n3. Check Balance\n4. Exit")
        choice = input("Choose an option: ")
        if choice == "1":
            account.deposit()
        elif choice == "2":
            account.withdraw()
        elif choice == "3":
            print(f"Current balance: ${account.balance:.2f}")
        elif choice == "4":
            break
        else:
            print("Invalid choice.")

if __name__ == "__main__":
    main()

Conclusion

The input() function is a powerful tool in Python for capturing user input, and converters like int() and float() allow you to work with this input in various data types. Understanding how to use these functions effectively is essential for building interactive and robust applications.

See you on the next post.

Sincerely,

Eng. Adrian Beria.