Sunday, 12 January 2025

Introduction to User Interaction in Python

 


User interaction in Python refers to the process of engaging with users by receiving input, processing it, and providing feedback or output. It is achieved using simple tools like the input() function for collecting data from users and the print() function for displaying results.

Example:

name = input("What is your name? ")

print(f"Hello, {name}!")

Python also supports advanced interaction, such as validating inputs, creating menus for choices, or even building graphical interfaces with libraries like tkinter. These features make Python programs user-friendly and interactive, suitable for various applications.

Python allows for user input.

That means we are able to ask the user for input.

The method is a bit different in Python 3.6 than Python 2.7.

Python 3.6 uses the input() method.

Python 2.7 uses the raw_input() method.


1.Ask for Input: Use the input() function to get information from the user.

name = input("What is your name? ")

print(f"Hello, {name}!")


2.Give Choices (Menu): Show options and let the user choose.

print("1. Say Hello\n2. Exit")

choice = input("Choose an option: ")

if choice == "1":

    print("Hello!")

elif choice == "2":

    print("Goodbye!")


3. Check Input: Make sure the user gives valid data.

age = input("Enter your age: ")

if age.isdigit():

    print("You entered a valid age!")

else:

    print("That’s not a number.")

4. Keep Asking (Loop): Use a loop to keep interacting until the user wants to stop.

while True:

    action = input("Type 'stop' to exit: ")

    if action.lower() == "stop":

        print("Goodbye!")

        break

    else:

        print(f"You typed: {action}")


F-Strings

F-string allows you to format selected parts of a string.

To specify a string as an f-string, simply put an f in front of the string literal, like this:

Example:

Create an f-string:

txt = f"The price is 49 dollars"

print(txt)


Placeholders and Modifiers

To format values in an f-string, add placeholders {}, a placeholder can contain variables, operations, functions, and modifiers to format the value.

Example

Add a placeholder for the price variable:

price = 59

txt = f"The price is {price} dollars"

print(txt)


A placeholder can also include a modifier to format the value.

A modifier is included by adding a colon : followed by a legal formatting type, like .2f which means fixed point number with 2 decimals:

Example

Display the price with 2 decimals:

price = 59

txt = f"The price is {price:.2f} dollars"

print(txt)


You can also format a value directly without keeping it in a variable:

Example

Display the value 95 with 2 decimals:

txt = f"The price is {95:.2f} dollars"

print(txt)

Perform Operations in F-Strings

You can perform Python operations inside the placeholders.


You can do math operations:

Example

Perform a math operation in the placeholder, and return the result:

txt = f"The price is {20 * 59} dollars"

print(txt)


You can perform math operations on variables:

Example

Add taxes before displaying the price:

price = 59

tax = 0.25

txt = f"The price is {price + (price * tax)} dollars"

print(txt)


You can perform if...else statements inside the placeholders:

Example

Return "Expensive" if the price is over 50, otherwise return "Cheap":

price = 49

txt = f"It is very {'Expensive' if price>50 else 'Cheap'}"

print(txt)


Execute Functions in F-Strings

You can execute functions inside the placeholder:

Example

Use the string method upper()to convert a value into upper case letters:

fruit = "apples"

txt = f"I love {fruit.upper()}"

print(txt)

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (87) AI (35) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (179) C (77) C# (12) C++ (83) Course (67) Coursera (231) Cybersecurity (24) Data Analytics (1) data management (11) Data Science (132) Data Strucures (8) Deep Learning (21) Django (14) Downloads (3) edx (2) Engineering (14) Euron (14) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Generative AI (4) Google (34) Hadoop (3) HTML Quiz (1) HTML&CSS (47) IBM (30) IoT (1) IS (25) Java (93) Java quiz (1) Leet Code (4) Machine Learning (62) Meta (22) MICHIGAN (5) microsoft (4) Nvidia (4) Pandas (4) PHP (20) Projects (29) pyth (1) Python (958) Python Coding Challenge (398) Python Quiz (54) Python Tips (3) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (4) Software (17) SQL (42) UX Research (1) web application (8) Web development (4) web scraping (2)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses