Using PythonAnywhere, write a program that meets the following criteria:Use an appropriate data structure to read in at least 10 integers from the user.Perform a variety of mathematical operations on the numbers, and print the results of the following:All the prime numbersThe greatest common divisor between the first and second numberSquare of the 3rd numberSquare root of the 8th numberThe absolute value of the 10th numberAsk the user to input a decimal, round it to 2 decimal places, and then overwrite the 4th number in the list.Change the 6th number to be the result of the following expression:The 5th number times the 3rd number divided by the 1st number (use integer division), all added to the 7th number.Use your new numbers to create a line chart using Pygal. Save the chart to a file called U7activity.svg. (Hint: instead of using pygal.HorizontalBar(), use pygal.Line(). You only need three lines of code to create this line chart with your list data.

1 answer

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:

  1. It reads in at least 10 integers from the user.
  2. It performs the required mathematical operations and prints the results.
  3. It takes a decimal input from the user and updates the 4th number accordingly.
  4. It computes a new value for the 6th number as per the provided expression.
  5. 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:

  1. Copy and paste the code into a new file (e.g., number_operations.py) in your PythonAnywhere Console or Code Editor.
  2. Make sure you have installed Pygal: pip install pygal.
  3. Run the script and follow the prompts to input your integers and decimal number.
  4. 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!