2016-12-21 1 views
0

私は、ユーザー入力番号を持つ必要のあるタスクが割り当てられており、ランダムな係数を持つ式が実行されます。ユーザーは、データをテキストファイルに保存するオプションを持っているはずです。私はこの時点までに課題を完了しましたが、ランダムな要因のためにテキストファイル上でデータが同じではないという問題に直面しています。ありがとう。ランダムな要素とそれらをPythonでエクスポートする

import random 
import os.path 
def main(): 
    print ("1.input numbers") 
    print ("2.run formula") 
print ("3.export data") 
maininput = int(input("Enter:")) 
if maininput == 1: 
    number() 
elif maininput == 2: 
    formula() 
elif maininput == 3: 
    file_check() 
elif maininput >3: 
    print ("invalid number") 
    return main() 

def number(): 
    number.n1 = int(input("first number:")) 
    number.n2 = int(input("second number:")) 
    main() 

def formula(): 
    randomfactor = random.uniform (0.8 ,0.5) 
    output = number.n1 * number.n2 * randomfactor 
    print (" answer:",round (output)) 
    main() 

def file_check(): 
    file_check.file_name = input("What would you like to name the file?") 
    if os.path.exists (file_check.file_name): 
     print ("A file with the same name already exists") 
     print ("Press 1 to overwrite") 
     print ("Press 2 to choose a new file name") 
     option = int(input("Enter: ")) 
     if option ==1: 
      export() 
     elif option ==2: 
      return file_check() 
     elif option >2: 
      return file_check() 
    if not os.path.exists(file_check.file_name): 
     export() 

def export(): 
    file = open (file_check.file_name,"w") 
    randomfactor = random.uniform (0.8 ,0.5) 
    output = number.n1 * number.n2 * randomfactor 
    exout = round (output) 
    file.write ("number:" + str(exout)+ '\n') 
    file.close 
    main() 




main() 
+0

インデントを修正できますか? – Holloway

+0

ランダムでもランダムでもかまいませんか?あなたはランダムを修正したいですか? –

+0

@ Jean-FrançoisFabreランダムは残しておかなければならないが、数字はテキストファイル – Josh

答えて

1

乱数を2回生成します。

def formula(): 
    randomfactor = random.uniform (0.8 ,0.5) 
    formula.output = number.n1 * number.n2 * randomfactor 
    print (" answer:",round (output)) 
    main() 

してから、ファイルに書き込むために、この結果を使用します:だから、formula.outputであなたの結果を保存することができ

def export(): 
    file = open (file_check.file_name,"w") 
    exout = round (formula.output) 
    file.write ("number:" + str(exout)+ '\n') 
    file.close 
    main() 

そして、ところで。あなたは変更することができますif not os.path.exists(file_check.file_name):else:

+0

私が大きなプロジェクトで私を助けることができるかどうか疑問に思っていましたか? – Josh

+0

どのようなお手伝いをしますか? –

+0

私の問題はこれと似ていましたが、私のコードに実装する方法がわかりません。 – Josh

関連する問題