2017-03-16 7 views
-3

最後の数時間私はPythonで簡単な数学のインタプリタを使ってプログラミング言語全体を理解しています。私の通訳はMentos(オペレーティングシステムEthanが本のJPodで作った名前を付けました)と呼ばれています。ひどく書かれたコードがあれば、私はこれを動作させようとしています。最適化は後で行います。今ここに私のコードは次のとおりです。私はそれをコンパイルすると私の通訳の問題

#Defining my main vars 

pointer = 0 
intstartvalue = 0 
intendvalue = 0 
intpointer = 0 
intlist = [] 
templist = [] 

def main(): 
    #This code takes the user input 
    print("Mentos> ", end="") 
    userinput = input("") 
    userinputvalue = [] 
    for x in userinput: 
     #Probally a better way to do this but meh 

     #Checks if x is a number 
     if x == ("1") or ("2") or ("3") or ("4") or ("5") or ("6") or ("7") or ("8") or ("9") or ("0"): 
      userinputvalue[x] += 1 #1 refers to an integer. I did intend to do this with a string in a list but it didn't work either 
     #Checks if x is a decimal point 
     elif x == ("."): 
      userinputvalue[x] += 2 #2 refers to a decimal point 
     #Checks if x is a operator 
     if x == ("*") or ("/") or ("+") or ("-"): 
      userinputvalue[x] += 3 #3 refers to a operator 

    #Will program this section when I can successfully store the input to memory 
    def add(): 
     pass 

    def sub(): 
     pass 

    def div(): 
     pass 

    def mul(): 
     pass 

    for c in userinputvalue: 

     if c == 1:   
      intstartvalue = c 
      #Great job me, embedded for loops 
      for x in userinputvalue: 
       if x == 3: 
        intendvalue = intendvalue - (c - 1) 
        intpointer = intstartvalue 
        #I intend to cycle through the input for numbers and and add it to a list, then join the list together 
        #To have the first number. 


if __name__ == "__main__": 
    main() 

私はエラーを取得する:

Traceback (most recent call last): 
    File "/home/echo/Mentos.py", line 46, in <module> 
    main() 
    File "/home/echo/Mentos.py", line 17, in main 
    userinputvalue[x] += 1 
TypeError: list indices must be integers or slices, not str 

私はこのエラーを取得し、どのようにそれを修正する理由を知りたいです。

+3

あなたの質問は何ですか? –

+0

具体的な質問がありますか? – mdurant

+0

なぜTraceback(最新の最後の呼び出し)を受け取るのですか: ファイル "/home/echo/Mentos.py"、行46、 main() ファイル "/home/echo/Mentos.py"第17行目のメイン userinputvalue [x] + =TypeError:リストインデックスはstrではなく整数またはスライスでなければなりません –

答えて

0

Checks if x is a number

いいえ、xが数字を含む文字列かどうかを確認します。それはinputが返されたものであるとして、あなたはx.isdigit()

をしたい

はその後、

list indices must be integers or slices, not str

まあ、xは、strです。

userinput = input("") # String 
for x in userinput: # characters, so still a string 

そして

Probally a better way to do this but meh

はい、あります。

# A dictionary, not a list, you can't index an empty list 
    userinputvalue = {i : 0 for i in range(10)} 


    for x in userinput: 
     #Checks if x is a number 
     if x.isdigit(): 
      pass 
     elif x == ".": 
      pass 
     elif x in {'*','/','+','-'}: 
      pass 

そして、私はinを使用しているため=>How do I test one variable against multiple values?

+0

これはうまくいきました。以前は辞書を使っていませんでした。ありがとう –

+0

また、他の言語のマップやハッシュと呼ばれる –

+0

これは、演算子ではなく数値でのみ機能します。私は あなたが 'userinputvalue [" + "] + = 3'で達成しようとしていることを知りません、例えば –