954,510 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Calculation without several condition statements

Hi! On school we got exercise to calculate how much the length of substance grow when we know original length and temperature change.
So,the case goes that i have value temperature coefficient of different substances and i have to multiply them. I'd like to it without doing several condition statements. I tried to do a class and function but it didn't work out.

Here's the code, and i don't have any clue how to finish it. It should calculate
like aluminium*1meter*1celsius and print out answer without using condition statements, but is there a way to do it?

#-*- coding: cp1252 -*-
aluminium = 0.000023
concrete = 0.000012
silver = 0.000019
gold = 0.000014
copper = 0.000017
glass = 0.000008


a = raw_input("enter substance: ")
l = float(raw_input("give original length: "))
t = float(raw_input("how much the temperature changes: "))

answer = a*l*t
print'length changes', l, 'meters'
Toikkala
Newbie Poster
6 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
 

I would instead of using seperate variables for the different materials, use a dictionary

#-*- coding: cp1252 -*-
substances = {
'aluminium':0.000023,
'concrete':0.000012,
'silver':0.000019,
'gold':0.000014,
'copper':0.000017,
'glass':0.000008,
}

a = raw_input("enter substance: ")
l = float(raw_input("give original length: "))
t = float(raw_input("how much the temperature changes: "))

answer = substances[a]*l*t
print answer


be aware that it will be case sensitive and Aluminium won't be recognised whereas aluminium is

Kruptein
Posting Whiz in Training
258 posts since Sep 2009
Reputation Points: 25
Solved Threads: 11
 


There are several ways to do this. First, you might want to use a menu, with a number input, instead of keying in "aluminium", etc. as they can be spelled differently (aluminum). Then use a dictionary or list of lists to chain the values to the raw material.

material_dict = {"concrete" : 0.000012,
                 "silver" : 0.000019,
                 "gold" : 0.000014 }     ## etc
# or
material_list = [ ["concrete", 0.000012], \
                  ["silver", 0.000019], \
                  ["gold", 0.000014] ]
woooee
Nearly a Posting Maven
2,454 posts since Dec 2006
Reputation Points: 777
Solved Threads: 714
 

Making it using dictionary seemed simpler to me (used it once). But thanks guys, i really appreciate ur help.

Toikkala
Newbie Poster
6 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
 

Generally speaking, a dictionary should be used instead of several if/elif/else statements. It is easier to read and understand, and easier to modify since entries can be added or deleted from the dictionary. You will probably use dictionaries quite often so try to become more familiar with the object over time.

woooee
Nearly a Posting Maven
2,454 posts since Dec 2006
Reputation Points: 777
Solved Threads: 714
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: