I have been assigned a task that requires me to have a user input numbers with then get ran through a formula with a random factor. the user should then have the option to save the data to a text file. I have completed the task up until this point but I am faced with the problem of the data not being the same on the text file because of the random factor, how can I over come this? thanks.
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 ()
Aucun commentaire:
Enregistrer un commentaire