Help writing a script
Write a script that takes two arguments the first being either pi/e and the second being an integer between 1 and 200. If the first argument is x and second argument is n then your script is expected to display the nth digit of x after the decimal point. The script should display a message if n is outside the range and exit elegantly. If the number of arguments is less than 2, then too the script should display an appropriate message and exit properly.
Trying in SageMath Jupyter notebook
import sys
if len(sys.argv) < 3:
print("Usage: python assn2a.py {pi/e} n")
sys.exit(1)
x = sys.argv[1]
n = int(sys.argv[2])
if n < 1 or n > 200:
print("n should be an integer between 1 and 200.")
sys.exit(1)
if x == "pi":
value = str(pi)
elif x == "e":
value = str(e)
else:
print("Invalid value for x. Please choose 'pi' or 'e'.")
sys.exit(1)
digit = value.split(".")[1][n-1]
print(digit)
Welcome to Ask Sage! Thank you for your question.
This looks like homework.
Try to describe where you are stuck and to isolate what you need help with.
Homework ?
What's the difference between exiting elegantly and exiting properly?