Sunday, October 29, 2023

CHAPTER 5 LOOP STRUCTURES

 

LEARN PYTHON THROUGH SERIES OF ROBOT'S ACTIVITIES 

In this chapter, the loop structures are explained with suitable examples. This example shows how Johnny, who is a robot, and what tasks he has to complete each day according to the requirement.

[In]list_duty = ['cleaning', 'cooking', 'arranging','ironing', 'washing', 'singing','playing','breakfast','lunch','dinner']

set_time=["morning","noon","evening", "night"]

for w in list_duty:

    a=input("get the time   ")

    if a == "noon" :

# if and only if, the chosen time is noon, the following duties are to be called. 

        a1=input("call the the followings  ")

        print("the duties are  ", a1)

        continue

    else:

        print("get the time again")

# The next program will select the time and duties simultaneously. 

[In]set_time=["morning","noon","evening", "night"]

[In]list_duty = ['cleaning', 'cooking', 'arranging','ironing', 'washing', 'singing', 'playing','breakfast','lunch','dinner']

[In]for w in list_duty:

    a=input("get the time   ")

    if a in set_time:

        a1=input("call the the followings  ")

        if a1 in list_duty:

           print("the duties are  ", a1)

           print("the duties are over")

        else:

            print("get the correct duty and time")

            continue

    else:

        print("get the correct time again")

[Out]get the time   noon

[Out]call the following _________

[Out]get the correct duty and time

Workout Problem 1:

[In]set_time=["morning","noon","evening", "night"]

[In]list_duty = ['cleaning','wash']

for w in list_duty:

    a=input("get the time   ")

    if a in set_time:

        a1=input("call the the followings  ")

        if a1 in list_duty:

           print("the duties are  ", a1)

           print("the duties are over")

        else:

            print("get the correct duty and time")

            continue

    else:

        print("get the correct time again")

[Out]get the time   noon

[Out]call the followings  lunch

# Here only 'cleaning', and 'washing' duties are provided.

[Out]get the correct duty and time

[Out]get the time   noon

[Out]call the following wash

[Out]the duties are   wash

[Out]the duties are over

Example 2:

#per day duties.

set_time=["morning","noon","evening", "night"]

list_duty = []

duties=input("assign duties ")

list_duty = duties.split()

# split command helps to retrieve individual items in the list.

print(list_duty)

for w in list_duty:

    a=input("get the time   ")

    if a in set_time:

        a1=input("call the the followings  ")

        if a1 in list_duty:

           print("the duties are  ", a1)

           print("the duties are over")

        else:

            print("get the correct duty and time")

            continue

    else:

        print("get the correct time again")

[Out]assign duties _____________

[In] cook, wash, study, pray

['cook,', 'wash,', 'study,', 'pray']

[Out]get the time   ___________

[In]noon

[Out]call the following ______

[In] cook, study

[Out]the duties are   cook, study

[Out]the duties are over

[Out]get the time   ___________

[In]morn

[Out]call the following ______

[In] study

[Out]the duties are  study

[Out]the duties are over

 

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 ...