Welcome, guest | Sign In | My Account | Store | Cart

A little tool that calculates temperature with a little user-friendly(ness) added to it.

Python, 53 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/python3

print('''Welcome to Temperature Calculator. You have three temperature option, 'Celsius', 'Fahrenhet' and 'Kelvin';
or if you wish to exit the program then type exit at any dialog box.''')

def tempCalc():
    selectionType = input('Select type of conversion: \n\na)"Celsius to Fahrenheit" or \n\nb)"Celsius to Kelvin" or \n\nc)"Fahrenheit to Celsius" or \n\nd)"Fahrenheit to Kelvin" or \n\ne)"Kelvin to Celsius" or \n\nf)"Kelvin to Fahrenheit" \n\nEnter Here:')
    if selectionType == ("Celsius to Fahrenheit") or selectionType == ("celsius to fahrenheit") or selectionType == ("CELSIUS TO FAHRENHEIT"):
        c_f = eval(input("\nEnter temperature in Fahrenheit to find Celsius: "))
        print ((1.8 * c_f) + 32)
    elif selectionType == ("Celsius to Kelvin") or selectionType == ("celsius to kelvin") or selectionType == ("CELSIUS TO KELVIN"):
        c_k = eval(input("Enter temperature in Celsius to find Kelvin: "))
        print(c_k + 273)
    elif selectionType == ("Fahrenheit to Celsius") or selectionType == ("fahrenheit to celsius") or selectionType == ("FAHRENHEIT TO CELSIUS"):
        f_c = eval(input("Enter temperature in Fahrenheit to find Celsius: "))
        print((f_c - 32) *(5/9))
    elif selectionType == ("Fahrenheit to Kelvin") or selectionType == ("fahrenheit to kelvin") or selectionType == ("FAHRENHEIT TO KELVIN"):
        f_k = eval(input("Enter temperature in Fahrenheit to Kelvin: "))
        print((5/9 * (f_k - 32) + 273))
    elif selectionType == ("Kelvin to Celsius") or selectionType == ("kelvin to celsius") or selectionType == ("KELVIN TO CELSIUS"):
        k_c = eval(input("Enter temperature in Kelvin to find Celsius: "))
        print(k_c - 273)
    elif selectionType == ("Kelvin to Fahrenheit") or selectionType == ("Kelvin to Fahrenheit") or selectionType == ("KELVIN TO FAHRENHEIT"):
        k_f = eval(input("Enter temperature in Kelvin to find Fahrenheit: "))
        print(((k_f - 273) * 1.8 ) + 32)
    elif selectionType == ('') or selectionType == (' '):
        print("Please make a selection. A blank input is not acceptable")
        tempCalc()
    elif selectionType == ("exit") or selectionType == ("Exit") or selectionType == ("EXIT") or selectionType == ("Quit") or selectionType == ("QUIT") or selectionType == ("quit"):
         exit()
    else:
        print("PLEASE RECHECK THE SPELLING, AND ENTER YOUR CHOISE AGAIN. THANK YOU")
        tempCalc()


tempCalc()

def loop():
    restartProgram = input('''
Would you like to try again?:''')
    while restartProgram == ("Yes") or restartProgram == ("yes") or restartProgram == ("YES"):
        return tempCalc()
    while restartProgram != ("Yes") or restartProgram != ("yes") or restartProgram != ("YES") or restartProgram != ("No") or restartProgram != ("no") or restartProgram != ("NO"):
        print ("Please reselect your decision again.")
        tempCalc()
    else:
        while restartProgram == ("No") or restartProgram == ("no") or restartProgram == ("NO"):
            print("thank you for using our calculator, hope to see you agin later")
            exit()


while True:
    loop()

I am not exactly sure why at the end I have "while True: loop()". Trust me it was pure instinct. I am new to Python, hoping to give in more recipes where I know what I write. However, would like someone to tell me what exactly is happening there.