私は、ユーザー入力番号を持つ必要のあるタスクが割り当てられており、ランダムな係数を持つ式が実行されます。ユーザーは、データをテキストファイルに保存するオプションを持っているはずです。私はこの時点までに課題を完了しましたが、ランダムな要因のためにテキストファイル上でデータが同じではないという問題に直面しています。ありがとう。ランダムな要素とそれらを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()
インデントを修正できますか? – Holloway
ランダムでもランダムでもかまいませんか?あなたはランダムを修正したいですか? –
@ Jean-FrançoisFabreランダムは残しておかなければならないが、数字はテキストファイル – Josh