Python: Raw Input and if statements -
i input variables utilize in formula (a, vi, d, t) via raw input , if has 4 run.
#!/usr/bin/python def formula(): formula = raw_input("what variables use?") if formula == 'a' , 'v' , 'd' , 't': homecoming 'd = vi*t + .5*a*t^2' else: homecoming 'wrong formula' print formula()
it isn't clear you're trying achieve, next might give ideas:
def select_formula(): equations = {frozenset(('a', 'vi', 'd' ,'t')): 'd = vi*t + .5*a*t^2'} variables = frozenset(input("enter variables use: ").split()) try: homecoming equations[variables] except keyerror: homecoming "no formula found" in use:
>>> select_formula() come in variables use: b c 'no formula found' >>> select_formula() come in variables use: d vi t 'd = vi*t + .5*a*t^2' points note:
a dictionary canonical python replacement turn long series ofelifs; the utilize of frozensets means user can come in variables in order; and the utilize of str.split() without argment means amount of whitespace between variables tolerated; if want e.g. comma-separated input, update split accordingly. python
No comments:
Post a Comment