我是编程新手,我编写了一个程序来解决方程中的不同变量。我有“if”“elif”和“else”设置来解决等式的不同部分。出于某种原因,它只会解决第一部分(“if”部分)我将复制并粘贴下面的程序。
import math
print 'A=Pert Calculator'
print ''
print 'Created by Triton Seibert'
print ''
Y = raw_input('What letter would you like to solve for?: ')
if Y == 'A' or 'a' or '1':
print 'Solving for A'
print ''
P = float(raw_input('Set value for P (initial investment):'))
e = 2.71828
print ''
r = float(raw_input('Set value for r (rate):'))
print ''
t = float(raw_input('Set value for t (time in years):'))
print ''
ert = e**(r*t)
answer = P*ert
print 'A equals:'
print answer
elif Y == 'P' or 'p' or '2':
print 'Solving for P'
print ''
A = float(raw_input('Set value for A (Final answer):'))
e = 2.71828
print ''
r = float(raw_input('Set value for r (rate):'))
print ''
t = float(raw_input('Set value for t (time in years):'))
print ''
answer = A / math.e**(r*t)
print 'P equals:'
print answer
elif Y == 'R' or 'r' or '3':
print 'Solving for r'
print ' '
A = float(raw_input('Set value for A (Final answer): '))
P = float(raw_input('Set value for P (initial investment):'))
e = 2.71828
print ' '
t = float(raw_input('Set value for t (time in years):'))
print ' '
almost = A/P
getting_there = math.log10(almost)/math.log10(e)
answer = getting_there/t
print 'r equals:'
print answer
elif Y == 'T' or 't' or '4':
print 'Solving for t'
print ' '
A = float(raw_input('Set value for A (Final answer): '))
P = float(raw_input('Set value for P (initial investment):'))
e = 2.71828
print ' '
r = float(raw_input('Set value for r (rate):'))
print ' '
#equation here (not done yet)
print 't equals:'
print answer
else:
print 'Not yet'
#change log to ln : log base e (x) = log base 10 (x) / log base 10 (e)
答案 0 :(得分:6)
此部分始终评估为True
:
if Y == 'A' or 'a' or '1':
它没有做你认为它做的事情;它这样做:
if (Y == 'A') or ('a') or ('1'):
和'a'
评估为True
,因此它会通过。你可能想要的是:
if Y in ['A', 'a', '1']: