Sunday, October 29, 2023

CHAPTER 6 LEARN FUNCTIONS

 LEARN PYTHON THROUGH SERIES OF ROBOT'S ACTIVITIES 

To repeat a particular action, reducing the generation of codes each time, and reusing codes generated once, is called operations in computer language. Activities are an event in which an action is initiated and completed within a specified time frame and this event is completed through specified procedures. For example, activities such as going to the store, returning from the store, writing an essay, and cooking can be considered. A shopping activity consists of various small tasks. Decide what to buy, decide how much of each item to buy, decide how much money you need to have, learn about giving money and buying the balance, get it all into a bucket, and take it home again.

This example shows how a robot can complete household chores promptly as an operation. Similarly, other operations may have several smaller tasks and conditions attached to them, such that when a robot is given a command, the commands are executed one after the other, and an operation is completed.

 

Ironing, Washing, Playing, Walking, Brushing, Gardening, Reading, Shopping, Cleaning the home, Cooking, Cleaning the kitchen, and singing.

Ironing function

    [In]def iron():

       dress_type=input("get the type of dress   ")

        dress_typelist=dress_type.split()

        print(dress_typelist)

        for x in dress_typelist:

            if x == "cotton":

           print("Maintain temperature at 60 deg")

                print("iron is over")

            elif x== "velvet":

            print("Maintain temperature at 30 deg")

                    print("iron is over")

            else:

                print("select correct cloth")

    [In]iron()

    [Out]get the type of dress   velvet

    ['velvet']

    [Out]Maintain temperature at 30 deg

    [Out] iron is over

    #Running the function again.

    [In]iron()

    [In]get the type of dress   cotton

    ['cotton']

    [Out]Maintain temperature at 60 deg

    [Out]iron is over

        #Running the function again.

 [In]iron()

    [In]get the type of dress   polyester

    ['polyester']

    [In]select correct cloth

Washing function

    #washing job

    def wash():

no_ofcloth=int(input("enter the no of dresses  "))

        while no_ofcloth<6:

            if no_ofcloth<3:

                waterlow=int(input("get the low water level   "))

                print("motor on. finish wash")

            else:

                waterhigh=int(input("get the high water level   "))

                print("motor on. finish wash")

            break

        else:

            print("get the low no of dresses")

    #Running the function.

    [In]wash()

    [In]enter the no of dresses  2

    [In]get the low water level   4

    [In]motor on. finish wash

    #Running the function again.

    [In]wash()

    [In]enter the no of dresses  5

    [In]get the high water level   5

    [In]motor on. finish wash

    #Running the function again.

    [In]wash()

    [In]enter the no of dresses  7

    [In]get the low no of dresses

    Autonomous Car Driving

         The following decisions were made before the car has to be moved from one place to another place. There were 4 sensors located beside the wheels of the car. If any sensor input is 0, no obstacle is assumed, False condition. If any sensor detects an obstacle, then the sensor input will be 1, True.

[In] def drive1():

         fls=str(input("front left sensor input   "))

         frs=str(input("front right sensor input   "))

    def forwbreak():

           print("stop the forward vehicle, apply brake")

    def forwacce():

         print("forward acceleration on")

    def forright():

        print("steer right")

    def forleft():

        print("steer left")

    def revbreak():

        print("stop the reversed vehicle, apply brake")

    def revacce():

        print("steer 4")

    if fls == "1" and frs == "1":

            forwbreak()

    elif frs=="1" and fls=="0":

            forleft()

    elif fls=="1" and frs=="0":

            forright()

    elif fls == "0" and frs == "0":

            forwacce()

    return

#When the function runs, based on the decision the car will be driven.

[In]drive1()

    front left sensor input   1

    front right sensor input   0

    steer right

[In] drive1()

    front left sensor input   1

    front right sensor input   1

    stop the forward vehicle, apply the brake

[In] drive1()

    front left sensor input   0

    front right sensor input   1

    steer left

[In]drive1()

    front left sensor input   0

    front right sensor input   0

    forward acceleration on

   #This example shown below is the function drive(), that is using 2 sensors. These 2 sensors will get the input from the user. As per user input, the decisions will be made. 

def drive():

        get_sendata=input(" the sensor data    ")

        sendata = get_sendata.split(",")

        print(sendata)

        a=type(sendata)

        b=len(sendata)

        print(a)

        print(b)

        a1=sendata[0]

        b2=sendata[1]

        print(a1)

        print(b2)

        if a1=="0" and b2=="1":

            print("turn left")

        elif a1=="1" and b2=="0":

            print("turn right")

        elif a1=="0" and b2=="0":

            print("accelerate")

        elif a1=="1" and b2=="1":

            print("stop the car")

        return

    When program runs

[In]drive()

    [Out] the sensor data    0,0

    ['0', '0']

    <class 'list'>

    [Out] 2

    [Out] 0

    [Out] 0

    Accelerate

[In]drive()

 

 

[Out] the sensor data    1,1

    ['1', '1']

    <class 'list'>

    [Out] 2

    [Out] 1

    [Out] 1

    [Out] stop the car

Practice Problem

If 4 sensors are fitted in a car, then the following program will be used.

[In]def drive():

         get_sendata=input(" the sensor data    ")

         sendata = get_sendata.split(",")

         print(sendata)

    #a1=front left , b2= front right, c3=rear right,     

        d4=rear left sensors.

        a=type(sendata)

    b=len(sendata)

    print(a)

    print(b)

    a1=sendata[0]

    b2=sendata[1]

    c3=sendata[2]

    d4=sendata[3]

    print(a1)

    print(b2)

    print(c3)

    print(d4)

    if ((a1=="0") and (c3=="0") and (d4=="0") and (b2=="1")):

        print("turn left and accelerate")

    elif ((b2=="1") and (c3=="1") and (d4=="1") and (a1=="0")):

        print("turn right and accelerate")

    elif a1=="0" and b2=="0":

        print("accelerate")

    elif c3=="1" and d4=="1":

        print("stop reverse the car")

    elif ((a1=="1") and (b2=="1") and (c3=="1") and (d4=="0")):

        print("reverse the car towards rear right")

    elif ((a1=="1") and (b2=="1") and (d4=="1") and (c3=="0")):

        print("reverse the car towards rear left")

    return

[In] drive()

[Out] the sensor data    0,0,0,1

['0', '0', '0', '1'] # list created.

<class 'list'>

4 # length of list elements, 4 elements.

0   # sensor data1

0   # sensor data2

0   # sensor data3

1   # sensor data4

accelerate

 

No comments:

Post a Comment

CHAPTER 18 EXPLORING THERMODYNAMICS WITH PYTHON: UNDERSTANDING CARNOT'S THEOREM AND MORE

  Python is a versatile programming language that can be used to simulate and analyze various physical phenomena, including thermal physics ...