0

私の問題は、方程式の両側のユーザー入力が変数(x)を含んでいることをプログラムが認識しないことです。これは、試行錯誤の方法で見つけています。xは、定義された変数xとは別の文字列と見なされます。したがって、ユーザーが式16の右辺を入力し、左辺をx * xと入力すると、プログラムはxを変数xとしてではなく、文字列xとして認識します。助けてください。とにかく、ここに私のコードがあります(これは、1つの変数タイプ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"
4

1 に答える 1

2

eval()任意のPythonコードを評価するために使用できます。ユーザーが実行できることを制限しようとしても、ユーザーが未知のコードを実行できるようにすることは非常に危険であるため、これを実際のコードで使用することは一般的に非常に悪い考えです。

ただし、使用する場合、 and引数を使用して、ユーザーが使用できる変数を制限することを強くお勧めします。例:globalslocals

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

変数xのみを明示的に定義することにより、ユーザーが想定外のものにアクセスしようとしないようにします。

>>> 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

数学関数の使用を許可する場合は、次のように2番目の引数でそれらを渡すことができます。

>>> import math
>>> eval('sin(x)', {'sin': math.sin}, {'x': x})
-0.95892427466313845
于 2012-10-16T00:45:04.323 に答える