Home > Back-end >  Why does my function return the same value without performing the required operations on it?
Why does my function return the same value without performing the required operations on it?

Time:09-27

My function doesn't work, I'm trying to build a simple program whit GUI. it can convert any temperature from any scale to the Kelvin scale by take from user a number of temperature and key to tell the function witch temperature scale you enter, this's a keys you can use it C or c == Celsius, K or k == kelvin, F or f == Fahrenheit and return [the key is unavailable] if the isn't one of them. But when I enter any key of these keys it's return the same value that I given it.

this is my code:

from tkinter import *
from tkinter import ttk
from turtle import width, window_width
from Science import Kelvin

window = Tk()

window.title('Chemical Physics')
window.geometry("300x200 10 20")

GetButton = ttk.Button(window, text = "--> Click here <--")
GetButton.pack()

EntryKelvinNumber = ttk.Entry(window, width = 40)
EntryKelvinNumber.pack()

EntryKelvinKey = ttk.Entry(window, width = 20)
EntryKelvinKey.pack()

def GetKelvinButton():
# the Kelvin function just return the same value 
    print(Kelvin(EntryKelvinNumber.get(),EntryKelvinKey.get()))
    
      
GetButton.config(command = GetKelvinButton)

window.mainloop()

this line from Science import Kelvin is import kelvin function from my own library.

this is a Kelvin function code:

def helpS():
    print("pleas use:\n")
    print("use 0 if your temperature is Kelvin\n")
    print("use 1 if your temperature is Celsius\n")
    print("use 2 if your temperature is Fahrenheit\n")
    #soon...

def Kelvin(Tempereture,key):
    if key == 'K' or 'k':
        return Tempereture
    elif key == 'C' or 'c':
        return Tempereture   273.15
    elif key == 'F' or 'f':
        i = (Tempereture - 32)/1.8
        i = i   273.15
        return i
    else:
        print("the key is unavailable")
        helpS()

I'm tryed to make the keys a numbers like 0 == Kelvin, 1 == Celsius, and 2 == Fahrenheit, But it still don't work.

CodePudding user response:

if key == 'K' or 'k':

is equivalent to

if (key == 'K') or ('k'):

which is equivalent to

if True:

since bool('k') is True.

You probably wanted to write

if key == 'K' or key == 'K':

Also, make sure Temperature variable is a number in your function Kelvin as EntryKelvinNumber.get() will return a string.

def Kelvin(Tempereture, key):
    Tempereture = float(Tempereture)
    # rest of Kelvin function

CodePudding user response:

This is just a problem with the syntax of Python, You will have to use the code below. You have to use the same conditional statement after 'or', just with a different value, if you are making that condition.

def Kelvin(Tempereture,key):
    if key == 'K' or key == 'k':
        return Tempereture
    elif key == 'C' or key == 'c':
        return Tempereture   273.15
    elif key == 'F' or key == 'f':
        i = (Tempereture - 32)/1.8
        i = i   273.15
        return I
    else:
        print("the key is unavailable")
        helpS()

print(Kelvin(50, "c"))

Hope this works.

  • Related