2017-11-09 14 views
1

これは初めてのことです。おそらく、コードを壊しているどこかで愚かなことをしています。いくつかの同様の記事を見てきましたが、助けになるものは何も見つかりませんでした。値が返されない

私が抱えている問題は、最後にnewmarblesを印刷しようとしたときに、定義されていないということです。私は値を返すという点で何か間違っていると思いますか?助けを前にありがとう。

import random 

def playNovice(marbles): 
    aimove = random.randint(1, (marbles/2)) 
    print("AI Move", aimove) 
    newmarbles = marbles - aimove 
    return newmarbles 

def userPlay(marbles): 
    usermove = int(input("Enter your move: ")) 
    while usermove > marbles/2 or usermove == 0: 
     print("Invalid Move") 
     usermove = int(input("Enter your move: ")) 
    else: 
     newmarbles = marbles - usermove 
     return newmarbles 


difficulty = input("Which difficulty? novice or expert: ") 
marbles = 100 
playNovice(marbles) 
userPlay(marbles) 
print(newmarbles) 

答えて

1

newmarblesのスコープはplayNovice()にローカルです。返された値は、同じ名前の変数に代入する必要があります。

newmarbles = playNovice(marbles) 
userPlay(marbles) 
print(newmarbles) 
1

return文は変数を返さず、値を返します。

newmarblesの値を返し、それを何もしないだろう、何が欲しいのは、変数aVariableに方法playNoviceの戻り値を代入します

int aVariable = playNovice(marbles); 
print(aVariable); 

このような何かを行うことです。

関連する問題