0

This program is meant to execute two programs based on user input. If the user inputs 1, it executes program_a. If the user inputs 2, it executes program_b. How do I make it so that if a user inputs any other integer, it randomly chooses between 1 or 2?

import random
choice = int(input("Enter the number for your choice: "))
r = random.randint(1,2)
if choice == 1:
  program_a()
elif choice == 2:
  program_b()
else:
  choice = r
N H
  • 1
  • 1

1 Answers1

1

You are looking to use random.choice to randomly choose between a list of data.

So, in your case:

import random
val = random.choice([1, 2])
idjaw
  • 25,487
  • 7
  • 64
  • 83