-5

I would like to input what variables I would use in the formula (a, vi, d, t) via raw input and only if it has all four will it run.

#!/usr/bin/python

def Formula():
    Formula = raw_input("What variables do you use?")
    if Formula == 'a' and 'v' and 'd' and 't':
        return 'd = vi*t + .5*a*t^2'
    else:
        return 'Wrong Formula'

print Formula()
  • This will provide [further reading](http://stackoverflow.com/questions/15112125/how-do-i-test-one-variable-against-multiple-values) though not exactly a duplicate. – Adam Smith Jun 26 '14 at 15:10
  • [Your if statement does not do what you think it does](https://stackoverflow.com/questions/15112125/how-do-i-test-one-variable-against-multiple-values). Also you are [name shadowing](https://en.wikipedia.org/wiki/Variable_shadowing) the function `Formula` with the string `Formula` – Cory Kramer Jun 26 '14 at 15:10
  • What do you expect the string `Formula` to look like once the user has entered it? `a == x and y and z` does *not* work the way you think. Do you want to insert the values into the equation string? Your code so far makes no sense; I suggest you revisit [the tutorial](https://docs.python.org/2/tutorial/). – jonrsharpe Jun 26 '14 at 15:11
  • Why all of the down votes? user has given the problem a credible shot. – tdelaney Jun 26 '14 at 15:15
  • 1
    Exactly how is the user entering the variables? Will they type "(a, vi, d, t)" or "a, vi, d, t" or "a vi d t" or "a,vi,d,t" or "avidt" or something else? – Kevin Jun 26 '14 at 15:17

2 Answers2

0

You could use sets to check. Subtracting two sets returns the members in the first set that aren't in the second set. The down side is that this does not check for unwanted input like "fred".

if set('avdt') - set(raw_input("What variables do you use?")):
    return 'Wrong Formula'
else:
    return 'd = vi*t + .5*a*t^2'
tdelaney
  • 73,364
  • 6
  • 83
  • 116
  • This won't work because one of the variables is two letters - `vi`. – jonrsharpe Jun 26 '14 at 15:16
  • @jonsharpe - from his first attempt, he clearly doesn't care about whether its 'vi' and he doesn't care about other bad characters either. If it turns out differently (I included a hint that its not terribly robust), then I can modify my answer. – tdelaney Jun 26 '14 at 15:19
0

It isn't clear what you're trying to achieve, but the following might give you some ideas:

def select_formula():
    equations = {frozenset(('a', 'vi', 'd' ,'t')): 'd = vi*t + .5*a*t^2'}
    variables = frozenset(input("Enter the variables to use: ").split())
    try:
        return equations[variables]
    except KeyError:
        return "No formula found"

In use:

>>> select_formula()
Enter the variables to use: a b c
'No formula found'
>>> select_formula()
Enter the variables to use: a d vi t
'd = vi*t + .5*a*t^2'

Points to note:

  • A dictionary is the canonical Python replacement for what could turn into a long series of elifs;
  • The use of frozensets means that user can enter the variables in any order; and
  • The use of str.split() without an argment means any amount of whitespace between variables is tolerated; if you want e.g. comma-separated input, update the split accordingly.
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437