Reloading python terminal after user input with a new output

Question:

I would like to create simple terminal app where user will have 4 choices. 3 numbers and one function that will create new 3 numbers and then create new terminal with newly created numbers. I tried terminal libraries , putting function into dictionary or list.

My question is how to call function so it will create desired output?

Here is my code so far:

import random

def main():
   def creation():
      num1 = random.randint(1,50)
      num2 = random.randint(1,50)
      num3 = random.randint(1,50)
      numbers = ['Again']
      numbers.append(num1)
      numbers.append(num2)
      numbers.append(num3)
      return numbers
   numbers = creation()
   
   while True:
       for numby in numbers:
           print(f'{numby} : {numbers.index(numby)}')
       choose = int(input('choose option: '))
       if choose > 0:
          print(numbers[choose])
          # continue task
       if choose == 0:
          main()
          break
main()

I want the output to look like this:

0 : Again
1 : num1 # for example 33
2 : num2 # for example 44
3 : num3 # for example 46
choose option: 

User types 0
and new menu will be generated

0 : Again
1 : num1 # now for example 12
2 : num2 # now for example 22
3 : num3 # now for example 38
choose option: 
Asked By: Painko

||

Answers:

I think this is what you’re after.

import random
def creation():
  numbers = ['Again']
  numbers.append(random.randint(1,50))
  numbers.append(random.randint(1,50))
  numbers.append(random.randint(1,50))
  return numbers

def main():   
   while True:
       numbers = creation()
       for numby, number in enumerate(numbers):
           print(f'{numby} : {number}')
       choose = int(input('choose option: '))
       if choose == 0:
          continue
       if choose > 0:
          print(numbers[choose])
          break
main()
Answered By: Tim Roberts
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.