DEV Community

Cover image for Creating and Managing User Accounts, Calculator & Sending Notifications in Python

Creating and Managing User Accounts, Calculator & Sending Notifications in Python

How to take inputs and validate them

We are starting by defining a function call welcomeNote

def welcomNote():
    name = input('What is your name ? : ')
    print("Hello, {}, 'welcome to G-Jeotech Innovation Technology".format(name))
    print("Kindly fill in the details \n")
    return name
Enter fullscreen mode Exit fullscreen mode

Here we are to get the name input be defining a new fun called profile

def profile():
    year  = int(input('What is the current year? (eg. 2020) ?: '))
    age = int(input('What year were you born: ?'))
    location = input("Your current address? : ") 
    d_o_b = year-age
    return age, location, d_o_b
Enter fullscreen mode Exit fullscreen mode

Then here we are to out the previous two functions by defining a new function called outputDate with multiple arguments

def outputData(name, age, location, d_o_b):
    print(f"Hello!, {name}")
    print("You were born in the year {}, therefore, you are {} years old".format(age,d_o_b))
    print(f"And your current home address : {location}\n")
Enter fullscreen mode Exit fullscreen mode

Lastly , we called the functions to displayed the inputs

name = welcomNote()

age,location,d_o_b = profile()

print("\nBelow is the brief summmary of your personal data in our database:\n")

outputData(name,age,location,d_o_b)
Enter fullscreen mode Exit fullscreen mode

## Find and replace function
_This simple Python program allows a user to replace a specific word in a sentence with another word of their choice. Here's how it works:

  1. User Input for Sentence:

The program prompts the user to input a sentence using input('Enter Your sentence:'). It then prints the original sentence entered.

2.Word Replacement:

The user is asked to specify the word they want to replace (word1) and the word they want to replace it with (word2).

  1. Word Replacement Action:

Using the replace() method, the program replaces all occurrences of word1 in the original sentence with word2 and displays the modified sentence.

Example:
Input sentence: "The sky is blue."
Replace word: "blue" with "green."
Output: "The sky is green."
_

sentence  = input('Enter You sentence :')
print("Your sentence is: ", sentence)

word1 = input("You are replacing  :")
word2 = input("With :")

print()
print(sentence.replace(word1, word2))
Enter fullscreen mode Exit fullscreen mode

## Users Inputs registration and Validation method

_This Python program simulates the process of creating an account and logging in with username and password validation. Here's how it works step by step:

  1. Create Account:

The program first asks the user to enter a username and a password using input(). These values are saved as variables (username and password).
After the user inputs the details, it prints a confirmation message: "Your account has been created successfully".

  1. Login Process:

The program then prompts the user to log in by asking for the username and password again.
This process is inside a while loop, which means it will continue until the correct login credentials are provided.

  1. Credential Validation:

In each iteration of the loop, the user is asked to input the username (namevali) and password (pwordvali).
If the entered username and password match the ones initially created by the user, the program prints "Logged in successfully" and exits the loop using the break statement.

  1. Handling Incorrect Credentials:

If the username and password do not match, the program prints "Invalid credentials, please try again." and continues to prompt the user for their login details.
The loop keeps running until the correct credentials are entered.
_

print("CREATE ACCOUNT")

print()
username = input("Enter username: ")
password = input("Enter password: ")

print("Your account has been created successfully")

print("Login now!")

# Start a loop to validate credentials until they match
while True:
    namevali = input("Enter username: ")
    pwordvali = input("Enter password: ")

    if username == namevali and password == pwordvali:
        print("Logged in successfully")
        break  # Exit the loop if credentials match
    else:
        print("Invalid credentials, please try again.")

Enter fullscreen mode Exit fullscreen mode

## Calculator Method 1

#Calculator code

#return x - y:
#this function add two numbers 
def addition(x, y):
    return x + y

#this function subtract two numbers 

def substract(x, y):
    return x - y

# This function multiplies two numbers
def multiply(x, y):
    return x * y

# This function divides two numbers
def divide(x, y):
    return x / y



print("Select operation.")
print("1.Add")
print("2.Subtract")
print("3.Multiply")
print("4.Divide")

# Take input from the user 
choice = input("Enter choice(1/2/3/4): ")

num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))

if choice == '1':
    print(num1,"+",num2,"=", addition(num1,num2))


elif choice == '2':
    print(num1,"-",num2,"=", substract(num1,num2))

elif choice == '3':
    print(num1,"*",num2,"=", multiply(num1,num2))

elif choice == '4':
    print(num1,"/",num2,"=", divide(num1,num2))
else:
    print("Invalid input/Choice")
Enter fullscreen mode Exit fullscreen mode

## Method 2:

fn = int(input("Enter first number : "))

sn = int(input("Enter second number :"))

opt = input("Your Entered Operator : ")

if opt == '+':
    print("The addition of ", fn, "and", sn, "is :",fn+sn)

if opt == '-':
    print("The subtraction of", fn, "minius", sn, "is :" ,fn-sn)


if opt == '*':
    print("The multiplication of" , fn, "times", sn, "is :",fn*sn)

if opt == '/':
    print("The division of", fn, "divided by", sn, "is :",fn/sn)

elif opt != opt:
    print('Enter the right operator')
else:
    print("")
Enter fullscreen mode Exit fullscreen mode

Lastly, what i want us to talk about is Notification Displayer

This script is used to send desktop notifications to the user. The notification can be customized with a title, message, and additional details (like the app name). You might use this for alerting users to important events, reminders, or updates from your application.

import time
from plyer import notification

# Delay for 5 seconds
#time.sleep(5)

# Display a notification
notification.notify(
    title='Hello',
    message='This Is a Notification from Joshua',
    app_name='G-Jeotech',
    timeout=30
)

Enter fullscreen mode Exit fullscreen mode

Feel free to watch my previous videos on software development, AI, ML, Data Science and, IT related topics.
You can encourage me by simply hitting the subscribe button to follow my YouTube page, like, and don't forget to drop your comment in the comment section.
https://www.youtube.com/@Ifechuku.G.JeoTech

Top comments (0)