Python Counting Program

How to create a python counting program


This is a Python 3 tutorial for creating a counting 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 is a simple while program that counts

number = int(input("Please provide a number between 1 and 100 that I can count to: "))

x = 1
 
if number >= x and number <= 100:

              while number >= x and number <= 100:
                             print(x)
                             x = x+1
              print("Thank You! I enjoyed counting to the number", number)


else:
              print("Sorry I can't count to that!")

The code below is slightly similar to the code above but with slight differences.


# this is a simple while program that counts

              number = int(input("Please provide a number between 1 and 100 that I can count to: \n "))

if number > 100 or number < 1:
              print("sorry I cant count to that!")          
              
else:
              x = 1

              while number >= x and number <= 100:
                             print(x)
                             x = x+1
              print("\nThank You! I enjoyed counting to the number", number)

These are two slightly different codes for counting in python, remember when coding in python indentation is very important when copying this code, you may want to ensure that the methods line up such as the if/else statements and the loops such as while and for.  

 


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.