2016-11-03 4 views
0

乱数ジェネレータであるコードの最初のビットを作ってから、それらの数値を減算しようとしています。私はこれまでのところ、これを持っている:raw_inputを使ってデータを評価して何かを返す

def rand2(): 
     rand2 = random.choice('123456789') 
     return int(rand2) 

    def rand3(): 
     rand3 = random.choice('987654321') 
     return int(rand3) 

私は、関数が一緒にこれらを入れています

def rand(): 
     print rand3() 
     print '-' 
     print rand2() 
     ans() 

私はANS()関数を追加することにより、ソルバーを作るしようとしています。

def ans(): 
     ans = int(raw_input("Answer: ")) 
     if ans == rand3() - rand2(): 
      print("Correct") 

ただし、これは正しい場合の返品のデータを評価しません。 raw_inputを取得して入力データを評価する際のヒントや提案はありますか?

+2

'rand3'と' rand2'の呼び出しは、* different * random値を返します。これらの結果を再度呼び出すのではなく変数に割り当てる必要があります。 –

答えて

1

rand2rand3あなたはその戻り値を保存する必要がありますので、このような何かが動作するはずです、呼び出しごとに異なる値を返す:

def rand(): 
    r3 = rand3() 
    r2 = rand2() 
    print r3 
    print '-' 
    print r2 
    ans(r3, r2) 

def ans(r3, r2): 
    ans = int(raw_input("Answer: ")) 
    if ans == r3 - r2: 
     print("Correct") 
0

ちょうどたら、あなたのランダム関数を呼び出すと、それらを渡します数値をパラメータとして使用します。例:

import random 

# Variable names changed. Having a variable the same name as a function 
# is confusing and can lead to side-effects in some circumstances 
def rand2(): 
    rand2v = random.choice('123456789') 
    return int(rand2v) 

def rand3(): 
    rand3v = random.choice('987654321') 
    return int(rand3v) 

# This functions takes as parameters the two random numbers 
def ans(r2, r3): 
    ans = int(raw_input("Answer: ")) 
    if ans == r3 - r2: 
     print("Correct") 

def rand(): 
    # This is the only place we create random numbers 
    r2 = rand2() 
    r3 = rand3() 

    # The print can be doe in one line 
    print r3, '-', r2 

    # Pass the numbers to ans() 
    ans(r2, r3) 

rand() 
関連する問題