2012-10-16 3 views
0

내 문제는 방정식의 측면에 대한 사용자 입력을 인식하지 못한다는 점입니다. 방정식의 변수가 포함되어 있습니다 (x). 찾는 데 시행 착오적 인 방법을 사용하고 있습니다. 정의 된 변수 x와는 별도로 문자열을 x로 봅니다. 따라서 사용자가 방정식 16과 x * x의 오른쪽을 왼쪽에 넣으면 프로그램은 x를 변수 x로 인식하지 않고 문자열 x로 인식합니다. 도와주세요.내 사용자 입력이 파이썬에서 변수를 허용하도록 만드는 방법은 무엇입니까?

startingLimit=raw_input("What is the lowest estimate that your variable could possibly be?") 

print 

wholeNumber=raw_input("Do you know if your variable will be a whole number or a    fraction? Answer: yes/no") 
if (wholeNumber== "yes"): 
    print 
    fraction= raw_input("Is it a decimal/fraction? Answer:yes/no") 
if (fraction=="yes"): 
    print 
    print "This program will only calculate up to the fourth place to the right of the decimal" 
    xfinder=0.0001 
else: 
    xfinder=1 

x=4   
leftEquation=raw_input("Enter your left side of the equation:") 
print 
rightEquation=raw_input("Enter the right side of the equation:") 
print 
amountSolutions=raw_input("How many solutions are there to your equation? (up to 20)") 




indivisualCount=0 
count=0 

while (count!=amountSolutions): 


if (count==1): 
    if (leftEquation==rightEquation): 
     a=x 
     count=count+1 
if (count==2): 
    if (leftEquation==rightEquation): 
     b=x 
     count=count+1 
if (count==3): 
    if (leftEquation==rightEquation): 
     c=x 
     count=count+1 
if (count==4): 
    if (leftEquation==rightEquation): 
     d=x 
     count=count+1 
if (count==5): 
    if (leftEquation==rightEquation): 
     e=x 
     count=count+1 
if (count==6): 
    if (leftEquation==rightEquation): 
     f=x 
     count=count+1 
if (count==7): 
    if (leftEquation==rightEquation): 
     g=x 
     count=count+1 
if (count==8): 
    if (leftEquation==rightEquation): 
     h=x 
     count=count+1 
if (count==9): 
    if (leftEquation==rightEquation): 
     i=x 
     count=count+1 
if (count==10): 
    if (leftEquation==rightEquation): 
     j=x 
     count=count+1 
if (count==11): 
    if (leftEquation==rightEquation): 
     k=x 
     count=count+1 
if (count==12): 
    if (leftEquation==rightEquation): 
     l=x 
     count=count+1 
if (count==13): 
    if (leftEquation==rightEquation): 
     m=x 
     count=count+1 
if (count==14): 
    if (leftEquation==rightEquation): 
     n=x 
     count=count+1 
if (count==15): 
    if (leftEquation==rightEquation): 
     o=x 
     count=count+1 
if (count==16): 
    if (leftEquation==rightEquation): 
     p=x 
     count=count+1 
if (count==17): 
    if (leftEquation==rightEquation): 
     q=x 
     count=count+1 
if (count==18): 
    if (leftEquation==rightEquation): 
     r=x 
     count=count+1 
if (count==19): 
    if (leftEquation==rightEquation): 
     s=x 
     count=count+1 
if (count==20): 
    if (leftEquation==rightEquation): 
     t=x 


if (indivisualCount==0): 
    x=x+xfinder 
    indivisualCount=indivisualCount+1 

x=x+xfinder 



print 
print "Compiling..." 
time.sleep(3) 
if (amountSolutions==1): 
    print "Solutions= ",a 
if (amountSolutions==2): 
    print "Solutions= ",a,b 
if (amountSolutions==3): 
    print "Solutions= ",a,b,c 
if (amountSolutions==4): 
    print "Solutions= ",a,b,c,d 
if (amountSolutions==5): 
    print "Solutions= ",a,b,c,d,e 
if (amountSolutions==6): 
    print "Solutions= ",a,b,c,d,e,f 
if (amountSolutions==7): 
    print "Solutions= ",a,b,c,d,e,f,g 
if (amountSolutions==8): 
    print "Solutions= ",a,b,c,d,e,f,g,h 
if (amountSolutions==9): 
    print "Solutions= ",a,b,c,d,e,f,g,h,i,j 
if (amountSolutions==10): 
    print "Solutions= ",a,b,c,d,e,f,g,h,i,j,k 
if (amountSolutions==11): 
    print "Solutions= ",a,b,c,d,e,f,g,h,i,j,k,l 
if (amountSolutions==12): 
    print "Solutions= ",a,b,c,d,e,f,g,h,i,j,k,l,m 
if (amountSolutions==13): 
    print "Solutions=",a,b,c,d,e,f,g,h,i,j,k,l,m,n 
if (amountSolutions==15): 
    print "Solutions= ",a,b,c,d,e,f,g,h,u,j,k,l,m,n,o 
if (amountSolutions==16): 
    print "Solutions= ",a,b,c,d,e,f,g,h,u,j,k,l,m,n,o,p 
if (amountSolutions==17): 
    print "Solutions= ",a,b,c,d,e,f,g,h,u,j,k,l,m,n,o,p,q 
if (amountSolutions==18): 
    print "Solutions= ",a,b,c,d,e,f,g,h,u,j,k,l,m,n,o,p,q,r 
if (amountSolutions==19): 
    print "Solutions= ",a,b,c,d,e,f,g,h,u,j,k,l,m,n,o,p,q,r,s 
if (amountSolutions==20): 
    print "Solutions= ",a,b,c,d,e,f,g,h,u,j,k,l,m,n,o,p,q,r,s,t 


print 
print 
time.sleep(5) 
print "PROGRAM COMPLETED" 
+1

일부 관련이없는 조언을 : 당신이'('범위 (amountSolutions에서 카운트)'에 대한) 및 목록 ('솔루션 = []'및 루프에 대해 배워야한다 solutions.append (뉴스 솔루션)'). 귀하의 코드는 이제 반복의 * 많은 *을 가지고 있으며, 그것은 거의 항상 당신이 문제를 한 수준 위로 생각할 필요가있다. – DSM

답변

2

당신은 임의의 파이썬 코드를 평가하는 eval()을 사용할 수 있습니다 : 어쨌든, 여기 내 코드는, (이것은, X 단 하나의 변수 유형으로 방정식에 대한 해결하기 위해 의미입니다). 실제 코드에서 이것을 사용하는 것은 일반적으로 아주 나쁜 생각입니다. 사용자가 수행 할 수있는 일을 제한하려고하더라도 사용자가 알 수없는 코드를 실행할 수있게하는 것은 매우 위험합니다. 당신이이 그것을 사용하지 경우

그러나, 나는 사용자가 사용할 수있는 변수를 제한 할 globalslocals 인수를 사용하는 것이 좋습니다 강하게. 예 :

>>> eval('x*y', None, {'x': x}) 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
    File "<string>", line 1, in <module> 
NameError: name 'y' is not defined 

은 당신이 수학 함수를 허용 할 경우

>>> x=5 
>>> eval('x*x', None, {'x': x}) 
25 

만 명시 적으로 일을 액세스하려고하지 않는 사용자를 확인하면 X 변수를 정의함으로써 그들이이 안돼요 사용 당신과 같이 제 2 인수에 전달할 수 있습니다

>>> import math 
>>> eval('sin(x)', {'sin': math.sin}, {'x': x}) 
-0.95892427466313845 
관련 문제