Untitled
unknown
plain_text
2 years ago
1.4 kB
11
Indexable
py
def calculate_sum(x, n):
# Initialize a variable to store the running sum of the series
result = 0
# Iterate through the values of k from 1 to n (inclusive)
for k in range(1, n + 1):
# Calculate the current term of the series: k / x^k
term = k / (x ** k)
# Add the current term to the running sum
result += term
# Return the final summation result
return result
def main():
try:
# Ask the user to input the value of x as a floating-point number
x = float(input("Enter the value of x: "))
# Ask the user to input the value of n as an integer
n = int(input("Enter the value of n: "))
# Check if n is less than 1, which is not allowed in the summation formula
if n < 1:
raise ValueError("n should be a positive integer.")
# Calculate the summation using the provided values of x and n
summation = calculate_sum(x, n)
# Display the result of the summation
print(f"The summation is: {summation}")
# Handle ValueError for incorrect input (e.g., non-numeric values)
except ValueError as e:
print(f"Error: {e}")
# Handle other possible exceptions
except Exception as e:
print(f"An error occurred: {e}")
if __name__ == "__main__":
# Call the main function to start the program
main()Editor is loading...