Python Forum
question regarding user Inputs
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
question regarding user Inputs
#1
So I've dabbled with Python enough to try a few projects. The first one I'm writing is to convert Fahrenheit to Celcius or Celcius to Fahrenheit.

My plan is:
1. Ask for user input to select F or C to indicate what type this is.
2. Then ask for user input for the temperature that needs to be converted.
3. Perform the correct formula calculation based upon them selecting F or C, if C is selected I'll convert to F or vice versa.


below is step 1 but I'm having issues figuring out how to get a second input from the user for the actual temp to be converted. I'm finding tons of options to print an output from this input but no options to take this input and then request a second input. I'm thinking I need to use functions to perform this but again not sure. 

Thanks for any advice.

input_list = ["F", "C"]
    P1 = input("Select F or C from: " + ' '.join(input_list) + "\n")
    print (P1)
Reply
#2
Hello!
Just ask the user for the temperature. Then print temperature to F, new line, temperature to C

Indentation of your code is wrong. The input is too complicated

P1 = input("Convert to C/F (select one): ")
print (P1)
Comma instead of slash would be better: C, F
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
(Mar-30-2017, 03:29 AM)wavic Wrote: Hello!
Just ask the user for the temperature. Then print temperature to F, new line, temperature to C

Indentation of your code is wrong. The input is too complicated

P1 = input("Convert to C/F (select one): ")
print (P1)
Comma instead of slash would be better: C, F

Thanks for the advice that worked out great.

This is what my code looks like now. I've tested it and it works. 

If I wanted this to loop so they could come back and after they convert one temp convert a second by simply entering C again. I'd need a loop to perform this action correct? Again sorry for the n00b questions. 

P1 = input("Convert to C,F (select one): ")
print (P1)
if P1 == "c":
    Temp = int(input("What is your temperature?"))
    print((Temp * (9/5)) + 32)
elif P1 == "f":
    Temp = int(input("What is your temperature?"))
    print((Temp - 32) * (5/9))
    
Reply
#4
Add  'Q' for quit.

while True:
    P1 = input("Convert to C,F (select one), 'Q'=quit: ")
    print (P1)

    if P1.lower() == "c": # str.lower() ensure that it won't miss the upper case 'C'
        Temp = int(input("What is your temperature?"))
        print((Temp * (9/5)) + 32)
    elif P1.lower() == "f":
        Temp = int(input("What is your temperature?"))
        print((Temp - 32) * (5/9))
    elif P1.lower() == 'q':
        break
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#5
Wow thanks.

Good catch on the P1.lower. I tested and noticed the problem, my solution for the problem was more complex than yours. I changed the break to exit() so it will close when complete.

Now to play with this more and expand it to do more conversions.
Reply
#6
break, breaks the loop and since there is no more code for execution it causes the program to exit.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#7
So should I do a
break
then
exit()
?
Reply
#8
It's your choice  Cool Both works. Consider that just an exit() function is useful to leave the Python interpreter. But if you want to quit the program/script use sys.exit().
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#9
This will be for the chemists at my wife's work to use when I finish so exit will work better.

Thanks for the help.
Reply
#10
here is the latest code I've done. I need to clean it up some more but just thought I'd post it up. Also fishing for pointers or advice on how it looks so far.

import random
 
good_bye = ['A goodbye isn’t painful unless you’re never going to say hello again.', 'A man never knows how to say goodbye; a woman never knows when to say it.',
            'As contraries are known by contraries, so is the delight of presence best known by the torments of absence.', 'Be well, do good work, and keep in touch.',
            'Being strong sometimes means being able to let go.', 'But fate ordains that dearest friends must part.', 'All those moments will be lost in time, like tears in rain.']
 
def Conversion_type():
    while True:
        P1 = input("Is your conversion:[1]ML and OZ, [2] C and F, [3]Grams and OZ, [4]SG and API, [5] SG to PPG, [Q] to quit ")
        print (P1)
 
        if P1 == "1":
            Fluid_converter()
        elif P1 == "2":
            Temp_converter()
        elif P1 == "3":
            Grams_ounces()
        elif P1 == "4":
            API_Specific_gravity()
        elif P1 == "5":
            SG_PPG()
        elif P1.lower == "q":
            quit()
 
def Fluid_converter():
 
    while True:
        P1 = input("Convert to ML,OZ (select one), 'Q'=quit: ")
        print (P1)
 
 
        if P1.lower() == "ml":
            Volume = float(input("What is your Volume?"))
            answer = (Volume / 29.5735296)
            print(round(answer, 4))
        elif P1.lower() == "oz":
            Volume = float(input("What is your Volume?"))
            answer = (Volume * 29.5735296)
            print(round(answer, 4))
        elif P1.lower() == 'q':
            break
 
def Temp_converter():
    while True:
        P1 = input("Convert to C,F (select one), 'Q'=quit: ")
        print (P1)
 
        if P1.lower() == "c":
            Temp = float(input("What is your temperature?"))
            print(((Temp) * (9/5)) + 32)
        elif P1.lower() == "f":
            Temp = float(input("What is your temperature?"))
            print(((Temp) - 32) * (5/9))
        elif P1.lower() == 'q':
            break
 
def grams_ounces():
    while True:
        P1 = input("Convert to G,OZ (select one), 'Q'=quit: ")
        print (P1)
 
 
        if P1.lower() == "g":
            mass = float(input("What is your mass?"))
            answer = ((mass) / 28.35)
            print(round(answer, 4))
        elif P1.lower() == "oz":
            mass = float(input("What is your mass?"))
            answer = ((mass) * 28.35)
            print(round(answer, 4))
        elif P1.lower() == 'q':
            break
 
def API_Specific_gravity():
    while True:
        P1 = input("Convert [API] and [SG] (select one), 'Q'=quit: ")
        print (P1)
 
        if P1.lower() == "api":
            value = float(input("What is your value?"))
            answer = ((141.5)/((value)+ 131.5))
            print(round(answer, 4))
        elif P1.lower() == "sg":
            value = float(input("What is your value SG?"))
            answer = (((141.5)/(value)) - 131.5)
            print(round(answer, 4))
        elif P1.lower == 'q':
            break
                     
 
def SG_PPG():
 
    while True:
        P1 = input("SG or PPG, 'Q'=quit: ")
        print (P1)
 
 
        if P1.lower() == "sg":
            Value = float(input("What is your Value?"))
            answer = ((Value) * 8.33)
            print(round(answer, 4))
        elif P1.lower() == "ppg":
            Value = float(input("What is your Value?"))
            answer = (Value / 8.33)
            print(round(answer, 4))
        elif P1.lower() == 'q':
            break
 
print("Let's get this party started!")
Conversion_type()
print((random.choice)(good_bye))
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to write a response if a user inputs a string horuscope42 3 2,238 Apr-29-2020, 03:39 PM
Last Post: deanhystad
  Perminantly saving user inputs + being able to retrieve it ThatOneGuyNoOneKnowsCodes 1 1,926 Oct-23-2019, 11:28 PM
Last Post: DT2000
  Trying to prompt user for 2 inputs on one line pythonprogrammer 2 2,503 Sep-15-2019, 04:41 PM
Last Post: snippsat
  user inputs in constructing a dictionary Exsul 3 3,703 Apr-10-2019, 12:25 PM
Last Post: ichabod801
  unit testing a method that asks two user inputs() in console gebel 0 2,150 Apr-03-2019, 07:59 PM
Last Post: gebel
  User input question linuxnoob 9 4,940 Jul-12-2018, 01:56 PM
Last Post: linuxnoob
  code that takes inputs for user name amounts etc and then sends custom message shaumyabrata 5 5,309 Feb-12-2017, 11:37 AM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020