Tuesday, March 31, 2015

User Select Temperature Conversion Python Program - Python Program Tutorial - Python Tutorial

User Select Temperature Conversion Python ProgramUser Select Temperature Conversion Python Program


In this Python tutorial, we are going to show you how to improve our previous two Python programs that we built by adding a feature where the user can select their conversion. We will build a user select temperature conversion Python program. This program will actually combine our previous two programs into one program and make the program more user friendly.


Functions, Methods and Control Statements Used In This Python Tutorial


  • print()

  • input()

  • float()

  • If Statement

  • round()

  • .format()

Python Program Build


Step 1 – Open Text Editor and Save Project


We first have to open our text editor and save the project as usertemp.py.


Step 2 – Welcome Statement


We use a print() statement in this case to return the text ‘Welcome To Our Temp Conversion Program’.


#Welcome Statement

print('Welcome To Our Temp Conversion Program')

Step 3 - Users Conversion Selection


This steps we ask the user which type of temperature conversion they would like to perform. The user will be able to select their type by typing Celsius or Fahrenheit into the command line. We first create a variable called ‘temp’ and assign this variable to the user’s input.  To get the user’s input we use input() and tell them to enter one of two commands.


#Users Conversion Selection

print('Welcome To Our Temp Conversion Program')
temp = input("What temp would you like to convert to? Type Celsius or Fahrenheit. ")

Step 4 – User’s Temperature Input


This step we ask the user for their temperature and assign their input to the variable ‘userstemp’.  Create a variable called ‘userstemp’ and assign it to the users input().


#User's Temperature Input

print('Welcome To Our Temp Conversion Program')
temp = input("What temp would you like to convert to? Type Celsius or Fahrenheit. ")
userstemp = float(input("What is your temp to be converted. "))

Step 5 – Flow Statement “IF Statement”


In this step, we introduce an if statement which will help us return the proper conversion to the user. I know we have not been covered if statements yet which we will cover in full in our Python tutorials shortly. It is good to see some advanced features of the Python programming language as we move along.

First thing we need to do is create the if statement. On the first line of the if statement, we say if the user’s input is equal to celsius then run the code below which is our code from our previous tutorial celsius conversion. If it is not equal to celsius then move on to the next if statement(elif).


#Flow Statement "IF Statement"

print('Welcome To Our Temp Conversion Program')
temp = input("What temp would you like to convert to? Type Celsius or Fahrenheit. ")
userstemp = float(input("What is your temp to be converted. "))

if temp == 'Celsius':
cel = round((userstemp - 32) * 5 / 9, 1)
print("Your temp in Celsius is ".format(cel))

Step 6 – Flow Statement “elif Statement”


In this step, we need to create a elif statement which is part of the if statement. If the previous statement is not true, then the program will skip the code below and move on to our elif statement. For the elif statement we want to see if the user entered ‘Fahrenheit’ if the user entered ‘Fahrenheit’ then our block of code contained in the elif statement will run. This code is similar to our previous Python tutorial where we converted celsius to fahrenheit.


#Flow Statement "elif Statement"

print('Welcome To Our Temp Conversion Program')
temp = input("What temp would you like to convert to? Type Celsius or Fahrenheit. ")
userstemp = float(input("What is your temp to be converted. "))

if temp == 'Celsius':
cel = round((userstemp - 32) * 5 / 9, 1)
print("Your temp in Celsius is ".format(cel))
elif temp == 'Fahrenheit':
fah = round(userstemp * 9 / 5 + 32, 1)
print("Your temp in Fahrenheit is ".format(fah))

Step 7 – Flow Statement “else”


In the final step of our program we have an else statement.  If the user enters anything other than ‘Celsius’ or ‘Fahrenheit’ the code will run the else statement.  This happen if the user misspells ‘Celsius’ or enters a word we did not request.  The else statement contains a simple print() statement.


#Flow Statement 'else'

print('Welcome To Our Temp Conversion Program')
temp = input("What temp would you like to convert to? Type Celsius or Fahrenheit. ")
userstemp = float(input("What is your temp to be converted. "))

if temp == 'Celsius':
cel = round((userstemp - 32) * 5 / 9, 1)
print("Your temp in Celsius is ".format(cel))
elif temp == 'Fahrenheit':
fah = round(userstemp * 9 / 5 + 32, 1)
print("Your temp in Fahrenheit is ".format(fah))
else:
print("Whoops Something went wrong")

Step 8 – Run The Program


Open your terminal or command prompt. cd into your desktop and type ‘python3 usertemp.py’ this will run your Python program. Try it out to make sure it works if it does that is great if you run into problems check your code with ours. If it does work, then your goal is to break the program. When you break your program, you should take notes how the failures in your code and this is how we know where our code needs to improve.


#Run The Program

Thomass-MBP:desktop Tommy$ python3 userstemp.py
Welcome To Our Temp Conversion Program
What temp would you like to convert to? Type Celsius or Fahrenheit. Fahrenheit
What is your temp to be converted. 26.5
Your temp in Fahrenheit is 79.7

Awesome we created a program that will take a user’s command indicating which type of conversion and then we also will take the temperature that needs to be converted.  If you played around with the program you probably noticed there are some issues we could have if we released this program to the public.


Issues with our program


  1. User must type the proper word with proper capitalization if not we will get an error.

  2. If the user does not enter the conversion properly the program will still ask for the users temperature which will not indicate to the user that they entered the wrong command.

In the next tutorial we will make some changes to the code to we can make it more user friendly and more reliable.


If you have any questions about this Python tutorial building a User Select Temperature Conversion Python program please leave a comment below we are here to help you learn Python.


Source Code

 


 



No comments:

Post a Comment