Python For Loops

How to create a python for loops program


This is a Python 3 tutorial for creating a pounds-to-kilograms program, and in order to get started I always start with a comment about what the program that I am building is. 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)

else:
              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

Best Devices To Get Started With Programming

Having a good solid computer is vital to start coding, and HP offers solid laptops for coders at every level.