Showing posts with label structure. Show all posts
Showing posts with label structure. Show all posts

11/9/22

check data type for all elements in a list

For more codes click here 

#check data type for all elements in a list


a = [1,"pease", 4, "dog"]


for i in a:

    if isinstance(i, int):

        print(f"{i} is an integer")

    elif isinstance(i, str):

        print(f"{i} is a string")

    else:

        pass


10/1/22

Simple calculator using python and tkinter

 from re import sub

from tkinter import *


#creating a window
root =  Tk()

#decale the default variable
calc = ""

#definig the function
def addi():
    global calc
    calc += "+"
    pop['text'] = calc

def subi():
    global calc
    calc += "-"
    pop['text'] = calc

def divi():
    global calc
    calc += "/"
    pop['text'] = calc

def multipl():
    global calc
    calc += "*"
    pop['text'] = calc

def ze():
    global calc
    calc += "0"
    pop['text'] = calc

def on():
    global calc
    calc += "1"
    pop['text'] = calc

def tw():
    global calc
    calc += "2"
    pop['text'] = calc

def th():
    global calc
    calc += "3"
    pop['text'] = calc

def fo():
    global calc
    calc += "4"
    pop['text'] = calc

def fi():
    global calc
    calc += "5"
    pop['text'] = calc

def si():
    global calc
    calc += "6"
    pop['text'] = calc

def se():
    global calc
    calc += "7"
    pop['text'] = calc

def ei():
    global calc
    calc += "8"
    pop['text'] = calc

def ni():
    global calc
    calc += "9"
    pop['text'] = calc

def decii():
    global calc
    calc += "."
    pop["text"] = calc

def evali():
    global calc
    try:
        calc = eval(calc)
        pop["text"] = calc
        calc = ""
    except:
        pop['text'] = "AN ERROR OCCURRED"
        calc = ""

def cleari():
    global calc
    calc = ""
    pop["text"] = calc



#naming the window
root.title("A SIMPLE CALCULATOR")

#label and entry field
m = Label(root, text="CALCULATOR", font=(25))
pop = Label(root, text='', font=(50))

#operators box
adding = Button(root, text="+",height=2, width=5, command=addi)
subtracting = Button(root, text="-",height=2, width=5, command=subi)
dividing = Button(root, text="/",height=2, width=5, command=divi)
multiplying = Button(root, text="*",height=2, width=5, command=multipl)
equating = Button(root, text="=", height=10, width=13, command=evali)

#numbers
nine = Button(root, text="9", height=2, width=5, command=ni)
eight = Button(root, text="8", height=2, width=5, command=ei)
seven = Button(root, text="7", height=2, width=5, command=se)
six = Button(root, text="6", height=2, width=5, command=si)
five = Button(root, text="5", height=2, width=5, command=fi)
four = Button(root, text="4", height=2, width=5, command=fo)
three = Button(root, text="3", height=2, width=5, command=th)
two = Button(root, text="2", height=2, width=5, command=tw)
one = Button(root, text="1", height=2, width=5, command=on)
zero = Button(root, text="0", height=2, width=5, command=ze)
ccc = Button(root, text="C", height=2, width=5, command=cleari)
deci = Button(root, text=".", height=2, width=5, command=decii)

#placing on screen
m.place(x=190, y=0)
pop.place(x=100, y=30)

#placiing operators
adding.place(x=100, y=60)
subtracting.place(x=150, y=60)
dividing.place(x=200, y=60)
multiplying.place(x=250, y=60)
equating.place(x=300, y=60)

#placing digits
nine.place(x=100, y=100)
eight.place(x=150, y=100)
seven.place(x=200, y=100)
six.place(x=250, y=100)
five.place(x=100, y=140)
four.place(x=150, y=140)
three.place(x=200, y=140)
two.place(x=250, y=140)
ccc.place(x=100, y=180)
one.place(x=150, y=180)
zero.place(x=200, y=180)
deci.place(x=250, y=180)

root.resizable(False, False)
root.geometry("500x500")



root.mainloop()


9/11/22

Problem 2 - The game of Nims / Stones

 In this game, two players sit in front of a pile of 100 stones. They take turns, each removing between 1 and 5 stones (assuming there are at least 5 stones left in the pile). The person who removes the last stone(s) wins.


Write a program to play this game. This may seem tricky, so break it down into parts. Like many programs, we have to use nested loops (one loop inside another).


In the outermost loop, we want to keep playing until we are out of stones.


Inside that, we want to keep alternating players. You have the option of either writing two blocks of code, or keeping a variable that tracks the current player. The second way is slightly trickier since we haven't learned lists yet, but it's definitely do-able!


Finally, we might want to have an innermost loop that checks if the user's input is valid. Is it a number? Is it a valid number (e.g. between 1 and 5)? Are there enough stones in the pile to take off this many? If any of these answers are no, we should tell the user and re-ask them the question.


So, the basic outline of the program should be something like this:


TOTAL = 100


MAX = 5


pile = TOTAL # all stones are in the pile to start


while [pile is not empty]:


while [player 1's answer is not valid]:


[ask player 1]


[check player 1's input... is it valid?]


[same as above for player 2]


Note how the important numbers 100 and 5 are stored in a single variable at the top. This is good practice -- it allows you to easily change the constants of a program. For example, for testing, you may want to start with only 15 or 20 stones.


Be careful with the validity checks. Specifically, we want to keep asking player 1 for their choice as long as their answer is not valid, BUT we want to make sure we ask them at least ONCE. So, for example, we will want to keep a variable that tracks whether their answer is valid, and set it to False initially.


When you're finished, test each other's programs by playing them!


Problem 2 - Cafe menu

 Write a program that first displays a simple cafe menu (see example below), asks the user to enter the number of a choice, and either prints the appropriate action OR prints an error message that their choice was not valid.


Example output:


1. Soup and salad


2. Pasta with meat sauce


3. Chef's special


Which number would you like to order? 2


One Pasta with meat sauce coming right up!


Another example output:


1. Soup and salad


2. Pasta with meat sauce


3. Chef's special


Which number would you like to order? 5


Sorry, that is not a valid choice

9/9/22

Spelling Backwards

 Given a string as input, use recursion to output each letter of the strings in reverse order, on a new line.

Sample Input

HELLO

Sample Output

O

L

L

E

H

9/7/22

Car data

 You are working at a car dealership and store the car data in a dictionary:

car = {

    'brand': 'BMW',

    'year': 2018,

    'color': 'red'

}

Your program needs to take the key as input and output the corresponding value.

Sample Input

year

Sample Output

2018

6/30/22

Python Namespaces

 A namespace is a collection of names and the details of the objects that the names refer to.

Python PIP

 PIP is a package management system that is used to install and manage Python-based software packages.

Python as an OOPs

 For those of you who don't know, oop stands for Object-Oriented Programming. Is python an oop, and why?

Python Function

 Function is a collection of operations which can be easily accessed throughout a program. these operation accepts a variable and passes it through the stated operations to give an output.

Python Loops and Iterations

 Loops are used to iterate within a set of variables. This can either be a list, string, tuple etc.

Python Conditionals

Conditionals are used to carry out functions which are depending on set rules I e conditions. To work with conditionals, you'll need to declare the if ..... else statement. 

6/29/22

Python Operators

In programming, there are different types of operators with carry out various functionality.

6/21/22

Heading, Paragraphs and Comments

 Headings

HTML has 6 main categories of heading tags which varies in sizes which decreases as the number gets higher.

HTML Structure

 Saving your HTML file

All files are saved with extensions which helps to determine how they are ran.

HTML files are saved with .html

Blog Archive