Archive

Archive for November, 2018

Python code: Date Validation

November 29, 2018 Leave a comment



Python: Date Validation

As in any application there is lots of functions and procedures helping the application, some of these functions are directly related to the menu bar other are there just to work on back ground to perform a task and return something that another function need to complete an action.

Here is a python code to ask the user to input a date and check if the date is in the format we want to be.

I am new in python so maybe there is a code or a function to perform this, so if any one can help in this.


#python code: Date Validation

def valid_date():

my_date=input(‘Enter the date as dd/mm/yyyy :’)

try:

d,m,y=(my_date.split(‘/’))

if len(d)==1: d=’0’+d

if len(m)==1: m=’0’+m

if len(y)==2: y=’20’+y

if int(d)>31: print(‘Day must be less then 31’), brake

if int(m)>12: print(‘Mounth must be less then 12’), brake

my_date=d+’/’+m+’/’+y

return my_date

except:

print(‘Enter a valid date.’)

return ‘false’

vd=valid_date()
if vd == ‘false’ : valid_date()
else:
print(‘OK’, vd)

Advertisements

Python : Expenditure App

November 28, 2018 2 comments



Expenditure Application

User input ‘choice’:

In today post we will add the user input to our menu and based on the user input the app some function will run. As we said in the previous post The Menu we will use (9) to Exit from the application.

To read the user input we will use ‘input(‘something’)’ statement and The Menu def will return the choice back so our if statement will take us to another function, or EXIT if choice = 9, so let’s start.

As we can see in the last two lines of the_menu def, we have

choice = input(‘What you want to do? ‘) and return choice

Then in the main application body we will have while statement holding if statement as here:

choice = the_menu() # Here we are calling the menu and putting the return in the choice.
while choice != ‘9’ :

if choice == ‘1’:

# Calling “Add New Entry” function

choice = the_menu()

if choice == ‘2’:

# Calling “Delete an Entry” function

choice = the_menu()

if choice == ‘3’:

# Calling “Change an Entry” function

choice = the_menu()

if choice == ‘4’:

# Calling “Show Total Expenditure in a Year.” function

choice = the_menu()

if choice == ‘5’:

# Calling “Show Expenditure Details in a year.” function

choice = the_menu()

if choice == ‘9’:

choice = the_menu()

brack


Your code should be something like what we have in the image.

Python : Expenditure App

November 27, 2018 3 comments



Expenditure Application


The Menu:

Continuing parts of the Expenditure Application, here is the basic menu that we will use in our app. We will select (9) as Exit from the system.

So now, we have a json file with sample data and stored in a directory, also we have a file called “expen.py” and we will write the menu def in it and save it.


def the_menu(): # This is a menu

print(‘1. Add New Entry.’)

print(‘2. Delete an Entry.’)

print(‘3. Change an Entry.’)

print(‘4. Show Total Expenditure in a Year.’)

print(‘5. Show Expenditure Details in a year.’)

print(‘9. Exit.’)

# This line will ask the user to enter the operation he want to do.

choice = input(‘What you want to do? ‘)

return choice




In this app we will have 5 functions as our menu and maybe one or two functions to facilitate some calculations in background.


Follow me on Twitter … Click here

Python : Expenditure App

November 26, 2018 2 comments



Expenditure Application


leveling the Ground:

In this post we will assume that we have a folder for our application, and we will create two files one is for our python code and we will name it “expen.py”, the other is for our data and we will name it “expen_dat.json” this file extension is ‘json’. Also I assume that we have json pack is installed in the PC so if you don’t have json this is the time to install it.

json file:After you create expen_dat.json file just open it with any text editor and copy a test data of our expenditure as shown.

{“expenditure”:
[
{“date”:”02/03/2016″,”amount”:300},
{“date”:”10/04/2016″,”amount”:550},
{“date”:”02/05/2016″,”amount”:300},
{“date”:”10/04/2016″,”amount”:550},
{“date”:”02/03/2016″,”amount”:300},
{“date”:”10/04/2016″,”amount”:550},
{“date”:”05/01/2017″,”amount”:400},
{“date”:”10/02/2017″,”amount”:400},
{“date”:”15/03/2017″,”amount”:400},
{“date”:”20/04/2017″,”amount”:400}
}]

In next post we will start with coding and testing our Expenditure Application.

I will use the @pycharm to write the code and test it, but any other code editors can be used. Also, I assume we have Python and json installed in your PC.

Python Project: Face

November 25, 2018 Leave a comment

Drawing Face

In this fast python code we will draw a Face using 3 circles and one line.

We need to import a library for this called turtle, and we will use functions to help us in our mission, so let’s start.

#Python #code to #draw #face

import turtle
t=turtle.Turtle()
t.speed(9) #this will set the drawing speed to 9.

t.pencolor(“black”) #you can change the color to red, blue, or any other.

# Now we will create a functions taking circle size(s), the position (x,y) and our turtle name (t)

def d_c(t,s,x,y)
t.penup() #take pen up of the canvas.
t.goto(x,y) #goto position x,y
t.pendown()

t.circle(s) # draw the circle.

d_c(t,50,25,0) # call to draw the face
d_c(t,7,5,50) #call to draw the eye.
d_c(t,7,50,50) #call to draw second eye
t.penup()

#now we will draw the mouth
t.goto(15,20)
t.pendown()
t.goto(50,20)

Categories: Uncategorized

Python: Expenditure App

November 24, 2018 1 comment



Expenditure Application

In this post, we will go through the case study of our application.

‘ The Case’

We want to build an application to store and retrieve some data regard our expenditure, we will go as simple as two keys (date and amount), and we will save the data in a json file. So to view the data we need some functions to do some operations on the data such as Add, Delete, retrieve and change.

I will use the @pycharm to write the code and test it, but any other code editors can be used. Also, I assume we have Python and json installed in your PC.




Follow me on Twitter..

Python Project

November 23, 2018 Leave a comment

Python Project:

Two months back I start to learn #Python “Python is a #computer #programming #language “, you can say it grabbed me very quickly. I start to read some #sample #code from the net, then downloading the requirement and after 10 working days I was writing #codes in python and doing what I love Solving #mathematics puzzles with #python’.

So I decide to contribute in training or publishing python language by posting some codes block with explaining and comments, also by developing an application so we will learn on a real example.

I am publishing some codes on my Twitter (@h_ta3kees):

https://twitter.com/h_ta3kees?s=09

and I will do the same here on my blog.

The coming posts will be about the application that we will start develop I will call it ‘Expenditure App’.