A
A
Arenesun2014-09-21 23:40:50
Python
Arenesun, 2014-09-21 23:40:50

How to call a function n times, where n is given by the user?

The program reads one given formula in which the user enters two values ​​measured in the lab. The number of measurements is different for each, but the formula is always the same. Communication with the program through the console. Is it possible to make it so that when starting the program it asks for the number of measurements?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
FanKiLL, 2014-09-22
@Arenesun

how_many_times_run_func = int(input("Please enter how many timw you wont run formula: "))

You just need to wrap it in a try except so that if you enter not numbers, then catch the exception.
You can also validate there so that, for example, you enter a number no higher than 1000
if you use a python below version 3, then inputuse it instead raw_input
I haven’t written in python for a long time, maybe someone will correct it
class FormulaRunner:
  def ask_user_run_times():
    try:
      how_many_times_run_func = int(input("Please enter how many timw you wont run formula: "))
      return how_many_times_run_func
    except ValueError:
      print("Ooops value must be a number")
      return FormulaRunner.ask_user_run_times()

  def my_super_formula():
    print("toster.ru")

  
if __name__ == "__main__":
  run_times = FormulaRunner.ask_user_run_times()

  for _ in range(run_times):
    FormulaRunner.my_super_formula()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question