Certainly! Below is a Python program that fulfills the provided criteria. Before you run it, make sure you have access to the Pygal library in your PythonAnywhere environment. If you do not have it installed, you can install it using pip.
Here's how the program is structured:
- It reads in at least 10 integers from the user.
- It performs the required mathematical operations and prints the results.
- It takes a decimal input from the user and updates the 4th number accordingly.
- It computes a new value for the 6th number as per the provided expression.
- Finally, it creates a line chart using the updated list of integers and saves it to a file named
U7activity.svg
.
Here's the complete code:
import math
import pygal
# Function to check if a number is prime
def is_prime(num):
if num <= 1:
return False
for i in range(2, int(math.sqrt(num)) + 1):
if num % i == 0:
return False
return True
# Read in at least 10 integers from the user
numbers = []
for i in range(10):
num = int(input(f"Enter integer {i+1}: "))
numbers.append(num)
# Find all prime numbers
prime_numbers = [num for num in numbers if is_prime(num)]
print("Prime numbers:", prime_numbers)
# Calculate and print the GCD of the first and second numbers
def gcd(a, b):
while b:
a, b = b, a % b
return abs(a)
gcd_result = gcd(numbers[0], numbers[1])
print("Greatest common divisor of the first and second numbers:", gcd_result)
# Calculate and print the square of the 3rd number
square_of_third = numbers[2] ** 2
print("Square of the 3rd number:", square_of_third)
# Calculate and print the square root of the 8th number
sqrt_of_eighth = math.sqrt(numbers[7])
print("Square root of the 8th number:", sqrt_of_eighth)
# Calculate and print the absolute value of the 10th number
abs_of_tenth = abs(numbers[9])
print("Absolute value of the 10th number:", abs_of_tenth)
# Ask the user for a decimal and overwrite the 4th number
decimal_input = float(input("Enter a decimal value to overwrite the 4th number: "))
numbers[3] = round(decimal_input, 2)
# Change the 6th number based on the given expression
numbers[5] = (numbers[4] * numbers[2] // numbers[0]) + numbers[6]
# Print the updated list of numbers
print("Updated list of numbers:", numbers)
# Create a line chart using Pygal and save it to a file
line_chart = pygal.Line()
line_chart.title = 'Line Chart of Updated Numbers'
line_chart.x_labels = [str(i) for i in range(1, len(numbers) + 1)]
line_chart.add('Numbers', numbers)
# Save chart to a file
line_chart.render_to_file('U7activity.svg')
print("Line chart saved as U7activity.svg")
Instructions to Run:
- Copy and paste the code into a new file (e.g.,
number_operations.py
) in your PythonAnywhere Console or Code Editor. - Make sure you have installed Pygal:
pip install pygal
. - Run the script and follow the prompts to input your integers and decimal number.
- After completing the inputs, you will find the
U7activity.svg
file in your current directory.
This program will print the results of your calculations and save the chart based on the updated numbers. Enjoy coding!