Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
1200+ fellow developers rate and compare features of the top IDEs, like Visual Studio, Eclipse, RAD, Delphi and others, across 13 categories. Enjoy this FREE Download of the IDE User Satisfaction Study by Evans Data Corporation. Download Now!
  #1  
Old February 20th, 2004, 08:52 AM
briangw briangw is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 17 briangw User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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.

Reply With Quote
  #2  
Old February 20th, 2004, 10:21 AM
percivall percivall is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 133 percivall User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
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.

Reply With Quote
  #3  
Old February 20th, 2004, 11:07 AM
Boceifus's Avatar
Boceifus Boceifus is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 93 Boceifus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 2 h 12 m
Reputation Power: 5
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?!?!?

Reply With Quote
  #4  
Old February 20th, 2004, 11:20 AM
briangw briangw is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 17 briangw User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Quote:
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.

Reply With Quote
  #5  
Old February 20th, 2004, 12:00 PM
briangw briangw is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 17 briangw User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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.

Reply With Quote
  #6  
Old February 20th, 2004, 01:13 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 19 m 5 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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


Reply With Quote
  #7  
Old February 20th, 2004, 01:24 PM
briangw briangw is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 17 briangw User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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.

Reply With Quote
  #8  
Old February 20th, 2004, 05:06 PM
Boceifus's Avatar
Boceifus Boceifus is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 93 Boceifus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 2 h 12 m
Reputation Power: 5
this feels just wrong,but im gonna quote myself



Quote:
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 :)

Reply With Quote
  #9  
Old February 21st, 2004, 02:45 AM
Boceifus's Avatar
Boceifus Boceifus is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 93 Boceifus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 2 h 12 m
Reputation Power: 5
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.'

Reply With Quote
  #10  
Old February 21st, 2004, 11:58 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 19 m 5 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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.

Reply With Quote
  #11  
Old February 21st, 2004, 12:31 PM
briangw briangw is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 17 briangw User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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.

Reply With Quote
  #12  
Old February 21st, 2004, 07:38 PM
briangw briangw is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 17 briangw User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
I can't get it to work. How do you import equations into this? Am I even on the right track?

Reply With Quote
  #13  
Old February 22nd, 2004, 12:56 PM
briangw briangw is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 17 briangw User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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.'

Reply With Quote
  #14  
Old February 22nd, 2004, 03:23 PM
briangw briangw is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 17 briangw User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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.")

Reply With Quote
  #15  
Old February 22nd, 2004, 05:55 PM
netytan's Avatar