[go: up one dir, main page]

0% found this document useful (0 votes)
9 views2 pages

code2pdf_67db3fa7c7d6e

The document contains a Python script that performs three tasks: reversing a user-input string and counting its vowels, determining if a user-input number is even or odd, and sorting a list of integers using NumPy. It also includes instructions for setting up a virtual environment for NumPy installation. The script handles user input and errors appropriately.

Uploaded by

Sa Mu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views2 pages

code2pdf_67db3fa7c7d6e

The document contains a Python script that performs three tasks: reversing a user-input string and counting its vowels, determining if a user-input number is even or odd, and sorting a list of integers using NumPy. It also includes instructions for setting up a virtual environment for NumPy installation. The script handles user input and errors appropriately.

Uploaded by

Sa Mu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

# Question 1

user_string = input("Enter a string: ")

# Reversing the string


reversed_string = user_string[::-1]
print(f"Reversed string: {reversed_string}")

# Counting vowels (both uppercase and lowercase)


vowels = "aeiouAEIOU"
vowel_count = sum(1 for char in user_string if char in vowels)

print(f"Number of vowels: {vowel_count}")

# Question 2
# Accepting a number from the user
num = int(input("Enter a number: "))

# Checking if the number is even or odd


if num % 2 == 0:
print(f"The number {num} is Even.")
else:
print(f"The number {num} is Odd.")

# Question 3
# Importing NumPy
import numpy as np

# Step 1: Accepting a list of integers from the user


user_input = input("Enter a list of numbers separated by spaces: ")
numbers = list(map(int, user_input.split()))

# Step 2: Explaining Virtual Environment setup


print("Simulating Virtual Environment Setup...")
print("Step 1: Creating a virtual environment named 'sortenv'")
print("Command: python -m venv sortenv")

print("Step 2: Activating the virtual environment")


print("Command (Windows): sortenv\\Scripts\\activate")
print("Command (Mac/Linux): source sortenv/bin/activate")

print("Step 3: Installing NumPy in the virtual environment")


print("Command: pip install numpy")

# Step 3: Sorting the list using NumPy


sorted_numbers = np.sort(numbers)
print(f"Sorted list: {sorted_numbers.tolist()}")
import numpy as np

# Accept input from user


user_input = input("Enter a list of numbers separated by spaces: ").strip()

# Handle empty input


if not user_input:
print("No input provided. Please enter a valid list of numbers.")
else:
try:
# Convert input string to a list of integers
numbers = list(map(int, user_input.split()))

# Sorting the numbers using NumPy


sorted_numbers = np.sort(numbers)

print(f"Sorted list: {sorted_numbers.tolist()}")

except ValueError:
print("Invalid input! Please enter only numbers separated by spaces.")

You might also like