In: Computer Science
MATLAB QUESTION
Write a python program to request a positive float input from the user, x. The program should check for the value of x. If x is larger than or equal to 1.0, the program should display the value of x in addition to the string “is larger than or equal to 1” and then terminate the program. If x is less than 1 the program should calculate y such that: y = 1-x+x^2/2-x^3/3+x^4/4-x^5/5……-x^99/99+x^100/100 The program should then display the value of y and then terminate. Also, the program should use “try and except” to check for non-numerical values for x.
#try statement block to check if user entered x as float value
try:
#take x input value from user
x = float(input("x: "))
# check if x is greater than or equal to 1.0
if x >= 1.0:
#if x is larger than or equal to 1.0 then print corresponding message
print(x," is larger than or equal to 1")
else:
#we need to make a summation for y
#initialize y for initial value as 1
y = 1
#iterate until 100 terms
for i in range(1,101):
#make summation with the given corresponding expression
y = y - pow(-1,i)*(pow(x,i)/i)
print("y = ",y)
#if non-numeric value is entered it hits the except block
except ValueError:
print("You have entered a non-numeric x valueD")