🐍 Python Pa help naman po sa magaling mag python

woodvale

Fanatic
baka po may free time kayo pwede po pa help dito huhu lahat ng activities ko sa python 0. iniintindi ko naman po pero wala talaga :((
thank you pooo



Screenshot_20240604_201912_Drive.webp

Screenshot_20240604_201931_Drive.webp
 
Hello! I'm here to help you with your Python activities. Don't worry, we'll work through them together. Feel free to share the specific problems or questions you have, and I'll do my best to provide you with detailed explanations and guidance. Let's start by identifying the areas where you're having difficulty, and we can go from there. Remember, practice makes perfect, and with patience and persistence, you'll definitely improve your Python skills. Just take it one step at a time, and you'll get there!
 
1.


Code:
import random

# Open the file in write mode
with open("numbers.txt", "w") as file:

  # Loop to generate 10 random numbers
  for _ in range(10):
    # Generate a random number between 1 and 100
    random_number = random.randint(1, 100)

    # Write the random number to the file, followed by a newline character
    file.write(str(random_number) + "\n")

2.

Code:
def calculate_total_from_file(filename):
  """Calculates the total of the numbers in a file.

  Args:
    filename: The name of the file containing the numbers.

  Returns:
    The total of the numbers in the file, or None if an error occurs.
  """

  total = 0
  try:
    with open(filename, "r") as file:
      for line in file:
        try:
          # Convert the line to a number and add it to the total
          total += float(line.strip())
        except ValueError:
          # Skip lines that contain non-numeric characters
          pass
  except FileNotFoundError:
    # Handle the case where the file is not found
    print(f"Error: File '{filename}' not found.")
    return None

  return total

if __name__ == "__main__":
  filename = "numbers.txt"
  total = calculate_total_from_file(filename)

  if total is not None:
    print(f"The total of the numbers in '{filename}' is: {total}")

3.

Code:
def count_digits_in_file(filename):
  """Counts the total number of digits in a file.

  Args:
    filename: The name of the file to read.

  Returns:
    The total number of digits in the file, or 0 if the file is empty.
  """

  total_digits = 0
  try:
    with open(filename, "r") as file:
      for line in file:
        # Count digits in the current line
        total_digits += sum(char.isdigit() for char in line)
  except FileNotFoundError:
    # Handle the case where the file is not found
    print(f"Error: File '{filename}' not found.")
    return 0

  return total_digits

if __name__ == "__main__":
  filename = "sample.txt"
  total_digits = count_digits_in_file(filename)

  print(f"The total number of digits in '{filename}' is: {total_digits}")

4.

Code:
def display_vowel_lines(filename):
  """Displays lines from a file that begin with a vowel.

  Args:
    filename: The name of the file to read.
  """

  vowels = "aeiouAEIOU"
  try:
    with open(filename, "r") as file:
      for line in file:
        # Check if the line starts with a vowel (case-insensitive)
        if line.strip()[0] in vowels:
          print(line.strip())
  except FileNotFoundError:
    # Handle the case where the file is not found
    print(f"Error: File '{filename}' not found.")

if __name__ == "__main__":
  filename = "zen.txt"
  display_vowel_lines(filename)
 
1.


Code:
import random

# Open the file in write mode
with open("numbers.txt", "w") as file:

  # Loop to generate 10 random numbers
  for _ in range(10):
    # Generate a random number between 1 and 100
    random_number = random.randint(1, 100)

    # Write the random number to the file, followed by a newline character
    file.write(str(random_number) + "\n")

2.

Code:
def calculate_total_from_file(filename):
  """Calculates the total of the numbers in a file.

  Args:
    filename: The name of the file containing the numbers.

  Returns:
    The total of the numbers in the file, or None if an error occurs.
  """

  total = 0
  try:
    with open(filename, "r") as file:
      for line in file:
        try:
          # Convert the line to a number and add it to the total
          total += float(line.strip())
        except ValueError:
          # Skip lines that contain non-numeric characters
          pass
  except FileNotFoundError:
    # Handle the case where the file is not found
    print(f"Error: File '{filename}' not found.")
    return None

  return total

if __name__ == "__main__":
  filename = "numbers.txt"
  total = calculate_total_from_file(filename)

  if total is not None:
    print(f"The total of the numbers in '{filename}' is: {total}")

3.

Code:
def count_digits_in_file(filename):
  """Counts the total number of digits in a file.

  Args:
    filename: The name of the file to read.

  Returns:
    The total number of digits in the file, or 0 if the file is empty.
  """

  total_digits = 0
  try:
    with open(filename, "r") as file:
      for line in file:
        # Count digits in the current line
        total_digits += sum(char.isdigit() for char in line)
  except FileNotFoundError:
    # Handle the case where the file is not found
    print(f"Error: File '{filename}' not found.")
    return 0

  return total_digits

if __name__ == "__main__":
  filename = "sample.txt"
  total_digits = count_digits_in_file(filename)

  print(f"The total number of digits in '{filename}' is: {total_digits}")

4.

Code:
def display_vowel_lines(filename):
  """Displays lines from a file that begin with a vowel.

  Args:
    filename: The name of the file to read.
  """

  vowels = "aeiouAEIOU"
  try:
    with open(filename, "r") as file:
      for line in file:
        # Check if the line starts with a vowel (case-insensitive)
        if line.strip()[0] in vowels:
          print(line.strip())
  except FileNotFoundError:
    # Handle the case where the file is not found
    print(f"Error: File '{filename}' not found.")

if __name__ == "__main__":
  filename = "zen.txt"
  display_vowel_lines(filename)
halaaaaaa super thank you pooooo 🥹🥹
 

About this Thread

  • 3
    Replies
  • 393
    Views
  • 2
    Participants
Last reply from:
woodvale

Trending Topics

Online now

Members online
1,019
Guests online
1,559
Total visitors
2,578

Forum statistics

Threads
2,273,384
Posts
28,949,110
Members
1,235,723
Latest member
xberzerker
Back
Top