Help writing a script

asked 2023-05-28 14:31:07 +0200

updated 2023-06-06 15:08:08 +0200

slelievre gravatar image

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)
edit retag flag offensive close merge delete

Comments

Welcome to Ask Sage! Thank you for your question.

slelievre gravatar imageslelievre ( 2023-06-06 15:08:39 +0200 )edit

This looks like homework.

Try to describe where you are stuck and to isolate what you need help with.

slelievre gravatar imageslelievre ( 2023-06-06 15:09:23 +0200 )edit

Homework ?

Emmanuel Charpentier gravatar imageEmmanuel Charpentier ( 2023-06-06 18:40:46 +0200 )edit

What's the difference between exiting elegantly and exiting properly?

John Palmieri gravatar imageJohn Palmieri ( 2023-06-06 20:18:43 +0200 )edit