#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Posts
    17
    Rep Power
    0

    How to add multiple entries to a variable


    On an if/else statement, how to I say

    If temp = f, c, F, C

    in code? What I'm trying to do is to force the user to enter one of those 4 and if not, he will be prompted again to enter the correct one.

    Btw, are there any good help documents on the web to assist me? The textbook is pretty lame and having a good tutorial or a site that lists good examples of basic commands would be nice.
    Last edited by briangw; February 20th, 2004 at 08:57 AM.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    133
    Rep Power
    11
    I'm guessing f, c, F and C are the strings "f", "c", "F" and "C". Anyway, how you do it is:
    Code:
    if temp in "fcFC": ...
    In general, <object "in" sequence> will tell you if the object is in the sequence.
    Last edited by percivall; February 20th, 2004 at 10:30 AM.
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Posts
    103
    Rep Power
    11
    or you could do something like this(a handy trick i picked up from lazy yogi)


    Code:
    temp= raw_input(Please indicate Celsius or Fahrenheit)
    while temp != 'C' and temp != 'c' and temp != 'F' and temp !='f':
        temp=raw_input(Please indicate Celsius or Fahrenheit)
    if temp == 'C':
        your script
    elif temp == 'c':
        your script
    elif temp == 'F':
        your script
    elif temp == 'f':
        your script
    It is not important if the glass is half full or half empty.What is important,is who has been drinking from MY glass?!?!?
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Posts
    17
    Rep Power
    0
    Originally Posted by percivall
    I'm guessing f, c, F and C are the strings "f", "c", "F" and "C". Anyway, how you do it is:
    Code:
    if temp in "fcFC": ...
    In general, <object "in" sequence> will tell you if the object is in the sequence.
    I was separating them out and using or

    in

    if temp = "f" or "c" or "F" or "C"

    and it didn't work. I never knew you could do that inside the quotes. I'll check out yours and Boceifus' and see which one works best for me.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Posts
    17
    Rep Power
    0
    Boceifus,

    Is the second entry for temp=raw() there to prompt the user to enter the correct letter in, if they do not?

    ***EDIT***

    ok, it works, but now I'm having another problem


    Code:
    low = int(raw_input("Please enter a low temperature. "))
    high = int(raw_input("Please enter a high temperature. "))
    temp = raw_input("Please enter a F for Fahrenheit or a C for Celsius. ")
    
    
    if low >= high:
        high = raw_input("Please type in a temperature that is higher than the low temperature. ")
    else:
        print range(low, high + 1)
    
    while temp != 'C' and temp != 'c' and temp != 'F' and temp !='f':
        temp = raw_input("Please indicate Celsius or Fahrenheit ")
    if temp == 'C':
        print
    elif temp == 'c':
        print   
    elif temp == 'F':
        print
    elif temp == 'f':
        print
    Why isn't the area that's in Bold not looping? When I get the prompt to put in a temp that is lower than the high one, I still put in a temp that is greater than the high one, but it exits out.
    Last edited by briangw; February 20th, 2004 at 12:43 PM.
  10. #6
  11. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    How do you mean, kinda comfused me a little there. What exactly is the problem? Or even what are you trying to do with the program?

    Anyway the while loop and if else statments could also be writen like this...

    Code:
    #!/usr/bin/env python
    
    temp = raw_input('Please indicate Celsius or Fahrenheit').upper()
    
    while not temp or temp not in 'CF':
    	temp = raw_input('Please indicate Celsius or Fahrenheit').upper()
    
    if temp == 'C':
    	print 'your script'
    elif temp == 'F':
    	print 'your script'
    
    raw_input()
    This Cuts down the amount of checks you have to do in the while and if statment since temp will be converted to upper case on assignment, so no more checking for lower case values .

    Mark.
    programming language development: www.netytan.com Hula

  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Posts
    17
    Rep Power
    0
    netytan,

    I have to write an application that takes in a low temp, high temp and a setting of Farenheit or Celsius. If the high temp is lower than the low one, I need to get the user to correct that. The output should be a 2 columned table with one of the types of temps on the left and the converted one on the right. After that, it returns to the start of the program and continues until the user types in "I quit" in the low temp field.

    What you displayed will not work, since we have not learned .upper() yet. My code is not looping for some reason; it's not continuing to give me the error that the high temp is still lower than the low one.
  14. #8
  15. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Posts
    103
    Rep Power
    11
    this feels just wrong,but im gonna quote myself



    Originally Posted by Boceifus
    or you could do something like this(a handy trick i picked up from lazy yogi)


    Code:
    temp= raw_input(Please indicate Celsius or Fahrenheit)
    while temp != 'C' and temp != 'c' and temp != 'F' and temp !='f':
        temp=raw_input(Please indicate Celsius or Fahrenheit)
    if temp == 'C':
        your script
    elif temp == 'c':
        your script
    elif temp == 'F':
        your script
    elif temp == 'f':
        your script
    what its doing:
    Code:
    temp=raw_input()      #asks for celsius or fahrenheit
    while temp != 'C':    #if the input is not a value we're looking for
        temp=raw_input()  #repeat the question
    if temp =='C':        #if the input is valid
        your code         #execute your code for this input
    and this is what i came up for your code:
    Code:
    while True:
        low = int(raw_input("Please enter a low temperature:"))
        high = int(raw_input("Please enter a high temperature:"))
        temp = raw_input('Please indicate "F" for Fahrenheit or "C" for Celsius:')
        while temp != 'C' and temp != 'c' and temp != 'F' and temp !='f':
            temp = raw_input('Please indicate "F" for Fahrenheit or "C" for Celsius:')
        if temp == 'C' and low <= high:
            print temp, (range(low, high + 1))
        elif temp == 'c' and low <= high:
            print temp, (range(low, high + 1))   
        elif temp == 'F' and low <= high:
            print temp, range(low, high + 1)
        elif temp == 'f' and low <= high:
            print temp, (range(low, high + 1))
        else:
            print 'Invalid temperatures specified!The low must be less than the high!'
    but as you prob guessed,i'm a Uber-Noob to Pyhton.Perhaps now that it's working as you want(provided i understood what you wanted hehe) maybe one of the "One Line" Guru's can help you to reduce it
    Last edited by Boceifus; February 20th, 2004 at 11:41 PM. Reason: first attempt was embarassingly wrong :)
    It is not important if the glass is half full or half empty.What is important,is who has been drinking from MY glass?!?!?
  16. #9
  17. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Posts
    103
    Rep Power
    11
    forgot the prompt to continue or exit

    Code:
    while True:
        low = int(raw_input("Please enter a low temperature:"))
        high = int(raw_input("Please enter a high temperature:"))
        temp = raw_input('Pleaseindicate "F" for Fahrenheit or "C" for Celsius:')
        while temp != 'C' and temp != 'c' and temp != 'F' and temp !='f':
            temp = raw_input('Please indicate "F" for Fahrenheit or "C" for Celsius:')
        if temp == 'C' and low <= high:
            print temp, (range(low, high + 1))
        elif temp == 'c' and low <= high:
            print temp, (range(low, high + 1))   
        elif temp == 'F' and low <= high:
            print temp, range(low, high + 1)
        elif temp == 'f' and low <= high:
            print temp, (range(low, high + 1))
        
        else:
            print 'Invalid temperatures specified!The low must be less than the high!'
    
        exit=raw_input('Hit <enter> to continue,or type "quit" to exit:')
        if exit == 'quit':
            break
        else:
            print 'Returning to main.'
    It is not important if the glass is half full or half empty.What is important,is who has been drinking from MY glass?!?!?
  18. #10
  19. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Heres what i came up with, This doesn't actually preform the calculations to convert between the two scales but the logic is very clear and i've tried to keep it to things you already know

    Code:
    #!/usr/bin/env python
    
    def options():
        print
        print 'F - Fahrenheit to Celsius.'
        print 'C - Celsius to Fahrenheit.'
        print 'Q - Quit the program.'
        print 'O - Show options.'
        print
    
    def fahrenheit():
        print '%d Celsius => %d Fahrenheit'
    
    def celsius():
         print '%d Fahrenheit => %d Celsius'
    
    if __name__ == '__main__':
    
        print 'Python Temperature converter - Give it a go!'
    
        options()
    
        while True:
    
            asked = raw_input('Please enter the option you want:\n')
    
            if asked == 'Q' or asked == 'q':
                break
            elif asked == 'C' or asked == 'c':
                celsius()
            elif asked == 'F' or asked == 'c':
                fahrenheit()
            elif asked == 'O' or asked == 'o':
                options()
    Note: You'll need to replace the celsius() and fahrenheit() functions so they ask the user for any values you need to calculate the different temps but it shouldn't be to hard.

    Mark.
    programming language development: www.netytan.com Hula

  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Posts
    17
    Rep Power
    0
    netytan,

    Thanks for the code, but I feel bad that you did all of that. Some of that won't work for me bsed on the assingment

    Here's my assignment:

    Write an application that will accept the following inputs: low temperature, high temperature, and type of temperature values that have been entered where F is for Fahrenheit and C is for Celsius. Be sure to use appropriate prompts and instructions for all inputs required.

    An output table will be created starting at the low temperature and ending with the high temperature with increments of one degree in the first column. In the second column the corresponding equivalent temperature of the other type will be listed.

    Be sure that the low temperature is indeed lower than the high temperature. Also the user must enter F or C in either upper or lower case and should be continuously queried until a valid value has been entered.

    Continue to generate tables for the user until the user enters “I quit” for the low temperature value.

    And here's my code, so far:

    Code:
    print "\nWelcome to the Temperature program" 
    
    while True:
        low1 = raw_input("Please enter a low temperature:")
        if low1 != 'I quit':
            low = int(low1)
        if low1 == 'I quit':
            break
    
        
        high = int(raw_input("Please enter a high temperature:"))
        temp = raw_input('Please indicate "F" for Fahrenheit or "C" for Celsius:')
        while temp != 'C' and temp != 'c' and temp != 'F' and temp !='f':
            temp = raw_input('Please indicate "F" for Fahrenheit or "C" for Celsius:')
        if temp == 'C' and low <= high:
            rnge = range(low, high + 1)
            other_temp =(9/5 * rnge) + 32  
            print temp, other_temp
        elif temp == 'c' and low <= high:
            print temp, (range(low, high + 1))   
        elif temp == 'F' and low <= high:
            print temp, range(low, high + 1)
        elif temp == 'f' and low <= high:
            print temp, (range(low, high + 1))
        
        
        else:
            print 'Invalid temperatures specified! The low must be less than the high!'
    
        exit = raw_input('Hit <enter> to continue,or type " I quit" in the low temp field to exit:')
        print 'Returning to main.'
    
    raw_input("\n\nPress the enter key to exit.")
    The area in bold is my main issue now. I've got to take the first columned range, convert it, then display it in a second column. Right now, it's not working, but I'm playing around with the code.
  22. #12
  23. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Posts
    17
    Rep Power
    0
    I can't get it to work. How do you import equations into this? Am I even on the right track?
  24. #13
  25. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Posts
    17
    Rep Power
    0
    OK, I got it working, but why is the conversion from Farenheit to Celsius not diplaying a list of 10 numbers? Instead, it is only doing 5.

    Code:
    print "\nWelcome to the Temperature program" 
    
    while True:
        low1 = raw_input("Please enter a low temperature: ").upper()
        if low1 != 'I QUIT':
            low = int(low1)
        if low1 == 'I QUIT':
            break
    
        
        high = int(raw_input("Please enter a high temperature: "))
        temp = raw_input('Please indicate Celsius or Fahrenheit ').upper()
    
        while not temp or temp not in 'CF':
    	temp = raw_input('Please indicate Celsius or Fahrenheit ').upper()
        
        if temp == 'C' and low <= high:
            
            low_other_temp = (9 / 5 * low) + 32
            high_other_temp = (9 / 5 * high) + 32
            rnge = range(low, high + 1)
            faren = range(low_other_temp, high_other_temp + 1)  
            print rnge
            print faren
       
        elif temp == 'F' and low <= high:
    
            num1 = low - 32
            num2 = high - 32
            low_temp = num1 * 5/9
            high_temp = num2 * 5/9
            rnge = range(low, high + 1)
            celsi = range(low_temp, high_temp + 1)
            print rnge
            print celsi
        
        else:
            print 'Invalid temperatures specified! The low must be less than the high!'
    
        exit = raw_input('Hit <enter> to continue. Type " I quit" in the low temp field to exit.')
        print 'Returning to main.'
  26. #14
  27. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Posts
    17
    Rep Power
    0
    I just wanted to thank everyone for their help. I finally got it.

    Here's the finished code:


    Code:
    print "\nWelcome to the Temperature program" 
    
    while True:
        low1 = raw_input("Please enter a low temperature: ").upper()
        if low1 != 'I QUIT':
            low = int(low1)
        if low1 == 'I QUIT':
            break
    
        
        high = int(raw_input("Please enter a high temperature: "))
        temp = raw_input('Please indicate Celsius or Fahrenheit ').upper()
    
        while not temp or temp not in 'CF':
         temp = raw_input('Please indicate Celsius or Fahrenheit ').upper()
        
        if temp == 'C' and low <= high:
            
            while low <= high:
             low_other_temp = (1.8*low+32)
             print low, low_other_temp
             low = low+1
       
        elif temp == 'F' and low <= high:
    
            while low <= high:
             low_other_temp = ((low - 32) * 5.0/9.0)
             print low, low_other_temp
             low = low+1
        
        else:
            print 'Invalid temperatures specified! The low must be less than the high!'
    
        exit = raw_input('Hit <enter> to continue. Type " I quit" in the low temp field to exit.')
        print 'Returning to main.'
    
    raw_input("\n\nPress the enter key to exit.")
  28. #15
  29. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    No problem GW, always happy to help!

    I was wondering how long it would take you to figure out you can't preform mathimatical operations on a list but i wasn't really sure on what you we're trying to do so, best to let you get on with it ...


    We learn as much from our failures as we do from our victories, if not more!
    Good luck with Python, and have fun!

    Mark.
    programming language development: www.netytan.com Hula


IMN logo majestic logo threadwatch logo seochat tools logo