Este código define una función convertir_unidades
que realiza las conversiones entre distintas unidades de medidas. Luego, el programa solicita al usuario la unidad de origen, la unidad de destino y el valor a convertir, y muestra el resultado de la conversión si es válida. Puedes agregar más conversiones al diccionario conversiones
según tus necesidades.
def convertir_unidades(valor, unidad_origen, unidad_destino):
conversiones = {
"cm_in": 0.393701,
"in_cm": 2.54,
"kg_lb": 2.20462,
"lb_kg": 0.453592
# Agrega más conversiones según necesites
}
clave = f"{unidad_origen}_{unidad_destino}"
if clave in conversiones:
factor = conversiones[clave]
resultado = valor * factor
return resultado
else:
return None
unidad_origen = input("Unidad de origen (cm, in, kg, lb): ")
unidad_destino = input("Unidad de destino (cm, in, kg, lb): ")
valor = float(input("Valor a convertir: "))
resultado = convertir_unidades(valor, unidad_origen, unidad_destino)
if resultado is not None:
print(f"{valor} {unidad_origen} equivale a {resultado} {unidad_destino}")
else:
print("Conversión no válida")
Versión simplificada:
conversiones = {"cm_in": 0.393701, "in_cm": 2.54, "kg_lb": 2.20462, "lb_kg": 0.453592}
unidad_origen = input("Unidad de origen (cm, in, kg, lb): ")
unidad_destino = input("Unidad de destino (cm, in, kg, lb): ")
valor = float(input("Valor a convertir: "))
clave = f"{unidad_origen}_{unidad_destino}"
resultado = valor * conversiones.get(clave, 1)
print(f"{valor} {unidad_origen} equivale a {resultado} {unidad_destino}")
Pueder ir al artículo principal:
Códigos Sencillos hechos en Python
Convierte fácilmente entre pulgadas, centímetros, milímetros y píxeles. Esta herramienta es esencial para diseñadores web, diseñadores gráficos, impresores y cualquier persona que necesite calcular dimensiones precisas entre unidades físicas y digitales. Convertidor de Unidades https://www.herramientasproductivas.com/pulgadas_cm_mm_pixeles.php
Public Class Calculadora
Dim a1, b1, T, a, b, c, v, m, volm, P, P1, P2, P3, P4, Ru, ac, bc As Decimal, VARIABLES DEL CODIGOOOOO
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
End
End Sub (BOTÓN 4 PARA CERRAR EL PROGRAMA)
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Me.Hide()
Dim instruccionesForm As New Instrucciones()
instruccionesForm.Show()
End Sub (BOTÓN 3 PARA REGRESAR A LAS INTRUCCIONES9
CODIGO PARA BLOQUEAR LOS UNICOS 2 TEXBOX
Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
ValidateKey2Press(sender, e)
End Sub, (ESTE ES DE LA PARTE 1)
Private Sub ValidateKey2Press(sender As Object, e As KeyPressEventArgs)
Dim textBox = DirectCast(sender, TextBox)
If Not Char.IsDigit(e.KeyChar) AndAlso e.KeyChar <> "." AndAlso Not Char.IsControl(e.KeyChar) Then
e.Handled = True
Return
End If
End Sub (ESTE ES LA PARTE 2 PARA FINALIZAR EL BLOQUE DEL PRIMER TEXTBOX)
Private Sub TextBox2_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox2.KeyPress
ValidateKeyPress(sender, e)
End Sub( BLOQUEO DEL SEGUNDO TEXTBOX)
Private Sub ValidateKeyPress(sender As Object, e As KeyPressEventArgs)
Dim textBox = DirectCast(sender, TextBox)
If Not Char.IsDigit(e.KeyChar) AndAlso e.KeyChar <> "." AndAlso e.KeyChar <> "-" AndAlso Not Char.IsControl(e.KeyChar) Then
e.Handled = True
Return
End If
End Sub (PARTE FINAL DE SU BLOQUEO)
ESTE ES PARA LAS LISTAS DE LOS COMBOBOX Y SOLO PERMITIR EL CONTELIDOO
Private Sub Calculadora(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
For Each Control As Control In Me.controls
If TypeOf control Is ComboBox Then
CType(control, ComboBox).DropDownStyle = ComboBoxStyle.DropDownList
End If
Next
Label6.Text = «» ‘ Asegúrate de que comience vacío
End Sub
ESTE CODIGO ES PARA EL BOTÓN DE LIMPIAR
Private Sub Button2_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button2.Click
TextBox1.Clear()
ComboBox1.SelectedIndex = -1
TextBox2.Clear()
ComboBox2.SelectedIndex = -1
ComboBox3.SelectedIndex = -1
ComboBox4.SelectedIndex = -1
Label6.Text = ""
End Sub
ACÁ COMIENZAN LOS PRIMEROS MENSAJES A PANTALLA Y ADVENTENCIA
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If ComboBox1.SelectedIndex = -1 Then
MessageBox.Show("Por favor, selecciona una unidad de volumen.", "Advertencia", MessageBoxButtons.OK, MessageBoxIcon.Warning)
Exit Sub
End If
If ComboBox2.SelectedIndex = -1 Then
MessageBox.Show("Por favor, selecciona una unidad de temperatura.", "Advertencia", MessageBoxButtons.OK, MessageBoxIcon.Warning)
Exit Sub
End If
If ComboBox3.SelectedIndex = -1 Then
MessageBox.Show("Por favor, selecciona un gas.", "Advertencia", MessageBoxButtons.OK, MessageBoxIcon.Warning)
Exit Sub
End If
If ComboBox4.SelectedIndex = -1 Then
MessageBox.Show("Por favor, selecciona una unidad de presión.", "Advertencia", MessageBoxButtons.OK, MessageBoxIcon.Warning)
Exit Sub
End If
If TextBox1.Text = "" Then
Label7.Text = MessageBox.Show("Por favor, complete todos los campos.", "Advertencia", MessageBoxButtons.OK, MessageBoxIcon.Warning)
v = 0
Label6.Text = ""
ElseIf TextBox1.Text = 0 Then
Label7.Text = MessageBox.Show("Error, el volumen no puede ser 0", "Advertencia", MessageBoxButtons.OK, MessageBoxIcon.Warning)
v = 0
Label6.Text = ""
Label9.Text = ""
ElseIf ComboBox1.Text = "cm3/g" Then
v = TextBox1.Text / 1000
Label7.Text = ""
Label9.Text = ""
ElseIf ComboBox1.Text = "ft3/lb" Then
v = TextBox1.Text / 16.0185
Label7.Text = ""
Label9.Text = ""
ElseIf ComboBox1.Text = "in3/oz" Then
v = TextBox1.Text / 1729.994
Label7.Text = ""
Label9.Text = ""
ElseIf ComboBox1.Text = "m3/kg" Then
v = TextBox1.Text * 1
Label7.Text = ""
Label9.Text = ""
Else
End If
ACÁ COMIENZAN LOS POSTERIORES MENSAJES A PANTALLA, TOMAR EN PALABRAS CLAVES LOS GASES
If TextBox1.Text = "" Then
MessageBox.Show("Agrega la variable de volumen.", "Advertencia", MessageBoxButtons.OK, MessageBoxIcon.Warning)
ElseIf TextBox2.Text = "" Then
MessageBox.Show("Por favor, complete todos los campos.", "Advertencia", MessageBoxButtons.OK, MessageBoxIcon.Warning)
ElseIf ComboBox3.Text = "Nitrógeno" Then
a1 = 0.02617
a = 136.2315
b1 = -0.00691
b = 0.05046
c = 42000
m = 28.013
volm = v * m
ElseIf ComboBox3.Text = "Helio" Then
a1 = 0.05984
a = 2.1886
b1 = 0
b = 0.014
c = 40
m = 4.0026
volm = v * m
ElseIf ComboBox3.Text = "Argón" Then
A1 = 0.02328
a = 130.78902
B1 = 0
b = 0.03931
c = 59900
m = 39.95
volm = v * m
Else
End If
If v > 0 And volm > 0 Then
Ru = 8.314
ac = a * (1 - (a1 / volm))
bc = b * (1 - (b1 / volm))
Else
End If
ACÁ COMIENZAN LOS MENSAJES A PANTALLA SOBRE LOS RANGOS
If TextBox2.Text = "" Then
Label8.Text = "Error, ingrese la temperatura"
P = 0
ElseIf ComboBox2.Text = "°C" Then
If TextBox2.Text >= -213.15 And TextBox2.Text <= 26.85 Then
T = TextBox2.Text + 273.15
Label8.Text = ""
If v > 0 Then
P1 = Ru * T / volm ^ 2
P2 = 1 - (c / (volm * T ^ 3))
P3 = volm + bc
P4 = ac / volm ^ 2
P = P1 * P2 * P3 - P4
Label6.Text = P
Label7.Text = " "
Label8.Text = " "
Label6.Text = Math.Round(P, 4).ToString
Else
End If
Else
Label8.Text = "La temperatura debe estar en un rango de: -213.15°C a 26.85°C"
Label6.Text = ""
P = 0
End If
ElseIf ComboBox2.Text = "°F" Then
If TextBox2.Text <= 80.33 And TextBox2.Text >= -351.67 Then
T = ((TextBox2.Text - 32) / 1.8) + 273.15
Label8.Text = ""
Label9.Text = ""
If V > 0 Then
P1 = Ru * T / volm ^ 2
P2 = 1 - (c / (volm * T ^ 3))
P3 = volm + bc
P4 = ac / volm ^ 2
P = P1 * P2 * P3 - P4
Label6.Text = P
Label7.Text = " "
Label8.Text = " "
Label6.Text = Math.Round(P, 4).ToString
Else
End If
Else
Label8.Text = "La temperatura debe estar en un rango de: -351.67°F a 80.33°F"
Label6.Text = ""
P = 0
End If
ElseIf ComboBox2.Text = "K" Then
If TextBox2.Text <= 300 And TextBox2.Text >= 60 Then
T = TextBox2.Text * 1
Label8.Text = ""
Label9.Text = ""
If v > 0 Then
P1 = Ru * T / volm ^ 2
P2 = 1 - (c / (volm * T ^ 3))
P3 = volm + bc
P4 = ac / volm ^ 2
P = P1 * P2 * P3 - P4
Label6.Text = P
Label7.Text = " "
Label8.Text = " "
Label6.Text = Math.Round(P, 4).ToString
Else
End If
Else
Label8.Text = "La temperatura debe estar en un rango de: 60K a 300K"
Label6.Text = ""
P = 0
End If
End If
ACA SIGUE LAS CONVERSIONES A UNIDADES EN EL COMBOX 4 Y EL UNICO MENSAJE A PANTALLA
If P < 0 Then
Label8.Text = «El volumen o la temperatura debe ser mayor para que la presión no sea negativa»
Label6.Text = «»
Else
If ComboBox4.Text = «Psi» Then
Label6.Text = Math.Round(P * 0.145038, 4).ToString
Label9.Text = «»
ElseIf ComboBox4.Text = «Kpa» Then
Label6.Text = Math.Round(P * 1, 4).ToString
Label9.Text = «»
ElseIf ComboBox4.Text = «Atm» Then
Label6.Text = Math.Round(P * 0.00986923, 4).ToString
Label9.Text = «»
ElseIf ComboBox4.Text = «Bar» Then
Label6.Text = Math.Round(P * 0.01, 4).ToString
Label9.Text = «»
ElseIf ComboBox4.Text = «Pa» Then
Label6.Text = Math.Round(P * 1000, 4).ToString
Label9.Text = «»
End If
End If
End Sub
ESTE CODIGO ES PARA PODER CONVERTIR LAS UNIDADES
Private Sub ComboBox4_SelectedIndexChanged1(sender As Object, e As EventArgs) Handles ComboBox4.SelectedIndexChanged
ConvertPressure()
End Sub
ESTE VA EN OTRA CASILLA PARA PODER COMPLETAR SU CONVERSIÓN
Private Sub ConvertPressure()
If P <= 0 Then
Label6.Text = «» ‘ Deja el label vacío si no se han ingresado valores válidos
ElseIf P < 0 Then
Label8.Text = «El volumen o la temperatura debe ser mayor para que la presión no sea negativa»
Else
Select Case ComboBox4.Text
Case «KPa»
Label6.Text = Math.Round(P * 1, 4).ToString
Case «Pa»
Label6.Text = Math.Round(P * 1000, 4).ToString
Case «Atm»
Label6.Text = Math.Round(P * 0.00986923, 4).ToString
Case «Bar»
Label6.Text = Math.Round(P * 0.01, 4).ToString
Case «Psi»
Label6.Text = Math.Round(P * 0.145038, 4).ToString
Case Else
Label6.Text = «» ‘ También considera vacío si no hay unidad seleccionada
End Select
End If
End Sub
End Class
.
Public Class Calculadora
Dim a1, b1, T, a, b, c, v, m, volm, P, P1, P2, P3, P4, Ru, ac, bc As Decimal
Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles PictureBox1.Click
End Sub
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
End
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Me.Hide()
Dim instruccionesForm As New Instrucciones()
instruccionesForm.Show()
End Sub
Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
ValidateKey2Press(sender, e)
End Sub
Private Sub ValidateKey2Press(sender As Object, e As KeyPressEventArgs)
Dim textBox = DirectCast(sender, TextBox)
If Not Char.IsDigit(e.KeyChar) AndAlso e.KeyChar <> "." AndAlso Not Char.IsControl(e.KeyChar) Then
e.Handled = True
Return
End If
End Sub
Private Sub TextBox2_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox2.KeyPress
ValidateKeyPress(sender, e)
End Sub
Private Sub ValidateKeyPress(sender As Object, e As KeyPressEventArgs)
Dim textBox = DirectCast(sender, TextBox)
If Not Char.IsDigit(e.KeyChar) AndAlso e.KeyChar <> "." AndAlso e.KeyChar <> "-" AndAlso Not Char.IsControl(e.KeyChar) Then
e.Handled = True
Return
End If
End Sub
Private Sub Calculadora(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
For Each control As Control In Me.Controls
If TypeOf control Is ComboBox Then
CType(control, ComboBox).DropDownStyle = ComboBoxStyle.DropDownList
End If
Next
Label6.Text = "" ' Asegúrate de que comience vacío
End Sub
Private Sub Button2_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button2.Click
TextBox1.Clear()
ComboBox1.SelectedIndex = -1
TextBox2.Clear()
ComboBox2.SelectedIndex = -1
ComboBox3.SelectedIndex = -1
ComboBox4.SelectedIndex = -1
Label6.Text = ""
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If ComboBox1.SelectedIndex = -1 Then
MessageBox.Show("Por favor, selecciona una unidad de volumen.", "Advertencia", MessageBoxButtons.OK, MessageBoxIcon.Warning)
Exit Sub
End If
If ComboBox2.SelectedIndex = -1 Then
MessageBox.Show("Por favor, selecciona una unidad de temperatura.", "Advertencia", MessageBoxButtons.OK, MessageBoxIcon.Warning)
Exit Sub
End If
If ComboBox3.SelectedIndex = -1 Then
MessageBox.Show("Por favor, selecciona un gas.", "Advertencia", MessageBoxButtons.OK, MessageBoxIcon.Warning)
Exit Sub
End If
If ComboBox4.SelectedIndex = -1 Then
MessageBox.Show("Por favor, selecciona una unidad de presión.", "Advertencia", MessageBoxButtons.OK, MessageBoxIcon.Warning)
Exit Sub
End If
If TextBox1.Text = "" Then
Label7.Text = MessageBox.Show("Por favor, complete todos los campos.", "Advertencia", MessageBoxButtons.OK, MessageBoxIcon.Warning)
v = 0
Label6.Text = ""
ElseIf TextBox1.Text = 0 Then
Label7.Text = MessageBox.Show("Error, el volumen no puede ser 0", "Advertencia", MessageBoxButtons.OK, MessageBoxIcon.Warning)
v = 0
Label6.Text = ""
Label9.Text = ""
ElseIf ComboBox1.Text = "cm3/g" Then
v = TextBox1.Text / 1000
Label7.Text = ""
Label9.Text = ""
ElseIf ComboBox1.Text = "ft3/lb" Then
v = TextBox1.Text / 16.0185
Label7.Text = ""
Label9.Text = ""
ElseIf ComboBox1.Text = "in3/oz" Then
v = TextBox1.Text / 1729.994
Label7.Text = ""
Label9.Text = ""
ElseIf ComboBox1.Text = "m3/kg" Then
v = TextBox1.Text * 1
Label7.Text = ""
Label9.Text = ""
Else
End If
If TextBox1.Text = "" Then
MessageBox.Show("Agrega la variable de volumen.", "Advertencia", MessageBoxButtons.OK, MessageBoxIcon.Warning)
ElseIf TextBox2.Text = "" Then
MessageBox.Show("Por favor, complete todos los campos.", "Advertencia", MessageBoxButtons.OK, MessageBoxIcon.Warning)
ElseIf ComboBox3.Text = "Nitrógeno" Then
a1 = 0.02617
a = 136.2315
b1 = -0.00691
b = 0.05046
c = 42000
m = 28.013
volm = v * m
ElseIf ComboBox3.Text = "Helio" Then
a1 = 0.05984
a = 2.1886
b1 = 0
b = 0.014
c = 40
m = 4.0026
volm = v * m
ElseIf ComboBox3.Text = "Argón" Then
A1 = 0.02328
a = 130.78902
B1 = 0
b = 0.03931
c = 59900
m = 39.95
volm = v * m
Else
End If
If v > 0 And volm > 0 Then
Ru = 8.314
ac = a * (1 - (a1 / volm))
bc = b * (1 - (b1 / volm))
Else
End If
If TextBox2.Text = "" Then
Label8.Text = "Error, ingrese la temperatura"
P = 0
ElseIf ComboBox2.Text = "°C" Then
If TextBox2.Text >= -213.15 And TextBox2.Text <= 26.85 Then
T = TextBox2.Text + 273.15
Label8.Text = ""
If v > 0 Then
P1 = Ru * T / volm ^ 2
P2 = 1 - (c / (volm * T ^ 3))
P3 = volm + bc
P4 = ac / volm ^ 2
P = P1 * P2 * P3 - P4
Label6.Text = P
Label7.Text = " "
Label8.Text = " "
Label6.Text = Math.Round(P, 4).ToString
Else
End If
Else
Label8.Text = "La temperatura debe estar en un rango de: -213.15°C a 26.85°C"
Label6.Text = ""
P = 0
End If
ElseIf ComboBox2.Text = "°F" Then
If TextBox2.Text <= 80.33 And TextBox2.Text >= -351.67 Then
T = ((TextBox2.Text - 32) / 1.8) + 273.15
Label8.Text = ""
Label9.Text = ""
If V > 0 Then
P1 = Ru * T / volm ^ 2
P2 = 1 - (c / (volm * T ^ 3))
P3 = volm + bc
P4 = ac / volm ^ 2
P = P1 * P2 * P3 - P4
Label6.Text = P
Label7.Text = " "
Label8.Text = " "
Label6.Text = Math.Round(P, 4).ToString
Else
End If
Else
Label8.Text = "La temperatura debe estar en un rango de: -351.67°F a 80.33°F"
Label6.Text = ""
P = 0
End If
ElseIf ComboBox2.Text = "K" Then
If TextBox2.Text <= 300 And TextBox2.Text >= 60 Then
T = TextBox2.Text * 1
Label8.Text = ""
Label9.Text = ""
If v > 0 Then
P1 = Ru * T / volm ^ 2
P2 = 1 - (c / (volm * T ^ 3))
P3 = volm + bc
P4 = ac / volm ^ 2
P = P1 * P2 * P3 - P4
Label6.Text = P
Label7.Text = " "
Label8.Text = " "
Label6.Text = Math.Round(P, 4).ToString
Else
End If
Else
Label8.Text = "La temperatura debe estar en un rango de: 60K a 300K"
Label6.Text = ""
P = 0
End If
End If
If P < 0 Then
Label8.Text = "El volumen o la temperatura debe ser mayor para que la presión no sea negativa"
Label6.Text = ""
Else
If ComboBox4.Text = "Psi" Then
Label6.Text = Math.Round(P * 0.145038, 4).ToString
Label9.Text = ""
ElseIf ComboBox4.Text = "Kpa" Then
Label6.Text = Math.Round(P * 1, 4).ToString
Label9.Text = ""
ElseIf ComboBox4.Text = "Atm" Then
Label6.Text = Math.Round(P * 0.00986923, 4).ToString
Label9.Text = ""
ElseIf ComboBox4.Text = "Bar" Then
Label6.Text = Math.Round(P * 0.01, 4).ToString
Label9.Text = ""
ElseIf ComboBox4.Text = "Pa" Then
Label6.Text = Math.Round(P * 1000, 4).ToString
Label9.Text = ""
End If
End If
End Sub
Private Sub ComboBox4_SelectedIndexChanged1(sender As Object, e As EventArgs) Handles ComboBox4.SelectedIndexChanged
ConvertPressure()
End Sub
Private Sub ConvertPressure()
If P <= 0 Then
Label6.Text = "" ' Deja el label vacío si no se han ingresado valores válidos
ElseIf P < 0 Then
Label8.Text = "El volumen o la temperatura debe ser mayor para que la presión no sea negativa"
Else
Select Case ComboBox4.Text
Case "KPa"
Label6.Text = Math.Round(P * 1, 4).ToString
Case "Pa"
Label6.Text = Math.Round(P * 1000, 4).ToString
Case "Atm"
Label6.Text = Math.Round(P * 0.00986923, 4).ToString
Case "Bar"
Label6.Text = Math.Round(P * 0.01, 4).ToString
Case "Psi"
Label6.Text = Math.Round(P * 0.145038, 4).ToString
Case Else
Label6.Text = "" ' También considera vacío si no hay unidad seleccionada
End Select
End If
End Sub
End Class