Uploaded by skimming-jaws-stew

AssignmentSeven Nissan Uddin

advertisement
Assignment Seven – Pint and Units
EGN3214
Nissan Uddin
July 05, 2024
Introduction
The objective of this code is to calculate the volume of a gas using the Ideal gas law. However,
the objective of this assignment is to use inputs and correct the user to make sure that an invalid
input cannot be entered.
Problem Statement
𝑃𝑉 = 𝑛𝑅𝑇
The ldeal gas law is displayed above and it will be used in the code. More specifically the
program will solve for V in liters. The program can take atm, mmHg, and PSI. In addition, it can
also take Celsius, Fahrenheit, and Kelvin to determine the final volume.
Methodology
This is the CalculateVolume function, and it does most of the math associated with the Ideal Gas
Law. The rest of the code is to determine if the unit is correct as well as what unit will be used in
the formula.
The units() function is able to prompt the user to input the desired units as well as display error if
an invalid input is given. For instance, the program can’t take strings and it will give an error for
doing do.
Assignment Seven – Pint and Units
EGN3214
Nissan Uddin
July 05, 2024
Solution
This solution was performed in colab and its able to prompt the user for the respective units
regarding pressure and temperature.
Conclusion
This program is significantly more flexible in its use compared to the previous iteration of the
program. It allows the user to input their desired units for temperature, pressure, and the moles of
the gas.
Appendix
def CalculateVolume(pressure, moles, temperature):
R = 0.0821 # Gas constant (L atm / mol K)
volume = (moles * R * temperature) / pressure
print("Volume of gas is {:.2f} liters".format(volume))
def units():
# Input pressure and unit
while True:
try:
pressure_unit = int(input("Enter pressure unit:\n1. atm\n2.
mmHg\n3. psi\n"))
if pressure_unit not in [1, 2, 3]:
raise ValueError
pressure = float(input("Enter the pressure value: "))
break
except ValueError:
print("Invalid input. Please enter 1, 2, or 3 for the pressure
Assignment Seven – Pint and Units
EGN3214
Nissan Uddin
July 05, 2024
unit and a valid number for the pressure.")
# Input moles
while True:
try:
moles = float(input("Enter the number of moles of gas: "))
break
except ValueError:
print("Invalid input. Please enter a valid number for the
moles.")
# Input temperature and unit
while True:
try:
temp_unit = int(input("Enter temperature unit:\n1. Celsius\n2.
Fahrenheit\n3. Kelvin\n"))
if temp_unit not in [1, 2, 3]:
raise ValueError
temperature = float(input("Enter the temperature value: "))
break
except ValueError:
print("Invalid input. Please enter 1, 2, or 3 for the temperature
unit and a valid number for the temperature.")
# Convert pressure to atm
if pressure_unit == 2: # mmHg
pressure *= 0.00131579
elif pressure_unit == 3: # psi
pressure *= 0.068046
# Convert temperature to Kelvin
if temp_unit == 1: # Celsius
temperature += 273.15
elif temp_unit == 2: # Fahrenheit
temperature = (temperature - 32) * 5/9 + 273.15
CalculateVolume(pressure, moles, temperature)
if __name__ == "__main__":
units()
Download