Example Of Python For Loops

How to create a python for loops program

This is a Python 3 tutorial for creating a pounds-to-kilograms program, loops can be beneficial to continuously run a bit of code until the user is complete with the java program. It is typically a good call to always have comments about the various parts of the program execution when run. By using the # key you are able to then create comments in Python.

# This program demonstrates For Loops

After the comment, I would usually then import the functions that I would utilize within the program. In this program, I will not utilize any functions.

Next, I will get input from the user; I will get the user's name and ask the user if they would like to say hello to their coworkers.

inp = input("What is your name? ")
hello = input("Hello do you want to say hello to everyone today! y/n: ")

We will then use if/else statements, the statement below means that if the user selects the option “y” then our loop condition will activate, which will print the hello message with every coworker’s name shown in the name variable. If the user does not select “y”, then the loop exits out and prints the “enjoy your day message”.

if hello == "y":
              names = ["Bob", "Eric", "Clare", "Chris", "Shawn", "Ashley"]
              for coworkers in names:
                             print("Hello", coworkers,"how are you doing today!")
              print("Thanks for saying hello to everyone!", inp)

              print("Enjoy your day!")

The code below also utilizes the python for loops method. This code converts pounds to kilograms. 

# This is a pound to Kilogram conversion program

kilo = 2.205

print("Hello this program will convert pounds into kilograms.\n")
print("This program will start at 1 and go through 50:\n ")
print("This program will round to the nearest whole number!\n")

for pounds in range(1, 51):
              print(pounds,"Pounds =",(pounds / kilo),"Kilograms")
print("\nThanks for using the pound converter!")

Enjoy this YouTube Tutorial on how to create a For Loops Python Programs 

No comments:

Post a Comment

Flag Quiz Game

Enjoy this free android app in the google play store. If you want to learn about the many country fl...