Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |
 User Name: Password: Remember me

New Free Tools on Dev Shed!
We're Excited to announce that Dev Shed now has 70 free tools on the site. To learn more, click here!

 Dev Shed Forums Sponsor:
#1
February 20th, 2004, 09:52 AM
 briangw
Registered User

Join Date: Feb 2004
Posts: 17
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 09:57 AM.

#2
February 20th, 2004, 11:21 AM
 percivall
Contributing User

Join Date: Jul 2003
Posts: 133
Time spent in forums: < 1 sec
Reputation 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 11:30 AM.

#3
February 20th, 2004, 12:07 PM
 Boceifus
Contributing User

Join Date: Jan 2004
Posts: 103
Time spent in forums: 3 Days 1 h 17 m 20 sec
Reputation Power: 10
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':
elif temp == 'c':
elif temp == 'F':
elif temp == 'f':
__________________
It is not important if the glass is half full or half empty.What is important,is who has been drinking from MY glass?!?!?

#4
February 20th, 2004, 12:20 PM
 briangw
Registered User

Join Date: Feb 2004
Posts: 17
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,

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.

#5
February 20th, 2004, 01:00 PM
 briangw
Registered User

Join Date: Feb 2004
Posts: 17
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 01:43 PM.

#6
February 20th, 2004, 02:13 PM
 netytan
Hello World :)

Join Date: Mar 2003
Location: Hull, UK
Posts: 2,537
Time spent in forums: 1 Week 2 Days 18 h 17 m 47 sec
Reputation Power: 68
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':
elif temp == 'F':

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

#7
February 20th, 2004, 02:24 PM
 briangw
Registered User

Join Date: Feb 2004
Posts: 17
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.

#8
February 20th, 2004, 06:06 PM
 Boceifus
Contributing User

Join Date: Jan 2004
Posts: 103
Time spent in forums: 3 Days 1 h 17 m 20 sec
Reputation Power: 10
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 21st, 2004 at 12:41 AM. Reason: first attempt was embarassingly wrong :)

#9
February 21st, 2004, 03:45 AM
 Boceifus
Contributing User

Join Date: Jan 2004
Posts: 103
Time spent in forums: 3 Days 1 h 17 m 20 sec
Reputation Power: 10
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.'```

#10
February 21st, 2004, 12:58 PM
 netytan
Hello World :)

Join Date: Mar 2003
Location: Hull, UK
Posts: 2,537
Time spent in forums: 1 Week 2 Days 18 h 17 m 47 sec
Reputation Power: 68
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.

#11
February 21st, 2004, 01:31 PM
 briangw
Registered User

Join Date: Feb 2004
Posts: 17
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.

#12
February 21st, 2004, 08:38 PM
 briangw
Registered User

Join Date: Feb 2004
Posts: 17
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?

#13
February 22nd, 2004, 01:56 PM
 briangw
Registered User

Join Date: Feb 2004
Posts: 17
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.'```

#14
February 22nd, 2004, 04:23 PM
 briangw
Registered User

Join Date: Feb 2004
Posts: 17
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.")```

#15
February 22nd, 2004, 06:55 PM
 netytan
Hello World :)

Join Date: Mar 2003
Location: Hull, UK
Posts: 2,537
Time spent in forums: 1 Week 2 Days 18 h 17 m 47 sec
Reputation Power: 68
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 ...

Quote:
 We learn as much from our failures as we do from our victories, if not more!

Good luck with Python, and have fun!

Mark.

 Viewing: Dev Shed Forums > Programming Languages > Python Programming > How to add multiple entries to a variable

## Developer Shed Advertisers and Affiliates

 Thread Tools Search this Thread Search this Thread: Advanced Search Display Modes Rate This Thread Linear Mode Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts vB code is On Smilies are On [IMG] code is On HTML code is Off
 View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox Forum Jump Please select one User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home -------------------- Programming Languages    PHP Development        PHP FAQs and Stickies    Perl Programming        Perl FAQs and Stickies    C Programming        C Programming FAQs and Stickies    Java Help        Java FAQs    Python Programming        Python Programming FAQs    Ruby Programming        Ruby Programming FAQs    Game Development        Game Development FAQs Programming Languages - More    ASP Programming        ASP Programming FAQs    .Net Development        .Net Development FAQs    Visual Basic Programming        Visual Basic Programming FAQs    Software Design        Software Design FAQs    ColdFusion Development        ColdFusion Development FAQs    Delphi Programming        Delphi Programming FAQs    Regex Programming        Regex Programming FAQs    XML Programming        XML Programming FAQs    Other Programming Languages        Other Programming Languages FAQs Web Design    HTML Programming        HTML Programming FAQs    JavaScript Development        JavaScript Development FAQs    CSS Help        CSS Help FAQs    Flash Help        Flash Help FAQs    Photoshop Help        Photoshop Help FAQs    Web Design Help        Web Design Help FAQs    Website Critiques        Website Critiques FAQs    Search Engine Optimization        Search Engine Optimization FAQs Mobile Programming    Mobile Programming        Mobile Programming FAQs    iPhone SDK Development        iPhone SDK Development FAQs    Android Development        Android Development FAQs    BlackBerry Development        BlackBerry Development FAQs Web Site Management    Business Help        Business Help FAQs    Development Software        Development Software FAQs    Scripts        Scripts FAQs Databases    Database Management        Database Management FAQs    DB2 Development        DB2 Development FAQs    MySQL Help        MySQL Help FAQs    PostgreSQL Help        PostgreSQL Help FAQs    Firebird SQL Development        Firebird SQL Development FAQs    MS SQL Development        MS SQL Development FAQs    Oracle Development        Oracle Development FAQs    LDAP Programming        LDAP Programming FAQs System Administration    Mail Server Help        Mail Server Help FAQs    Apache Development        Apache Development FAQs    Security and Cryptography        Security and Cryptography FAQs    Antivirus Protection        Antivirus Protection FAQs    DNS        DNS FAQs    IIS        IIS FAQs    Networking Help        Networking Help FAQs    FTP Help        FTP Help FAQs Operating Systems    BSD Help        BSD Help FAQs    Linux Help        Linux Help FAQs    UNIX Help        UNIX Help FAQs    Windows Help        Windows Help FAQs    Mac Help        Mac Help FAQs Web Hosting    Web Hosting        Web Hosting FAQs    Free Web Hosting        Free Web Hosting FAQs    Web Hosting Requests        Web Hosting Requests FAQs    Web Hosting Offers        Web Hosting Offers FAQs Computer Hardware    Computer Hardware    CPUs        CPUs FAQs    Cooling        Cooling FAQs    Embedded Programming        Embedded Programming FAQs    Motherboards        Motherboards FAQs    Multimedia Hardware        Multimedia Hardware FAQs Other    Dev Shed Lounge        Dev Shed Lounge FAQs    Development Articles        Development Articles FAQs    Beginner Programming        Beginner Programming FAQs    Hire A Programmer        Hire A Programmer FAQs    Project Help Wanted        Project Help Wanted FAQs Latest News Updated Hourly    Technology News    Business News    Science News Forum Information    Forum Rules/Guidelines        Forum Rules/Guidelines FAQs    Forum Announcements        Forum Announcements FAQs    Dev Shed Gaming Center        Go to the Dev Shed Battle Arena        Go to the Dev Shed Arcade Games        Go to the Legend of the Green Dragon    Suggestions & Feedback        Suggestions & Feedback FAQs

 Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |