8/28/23, 11:42 AM Untitled23 - Jupyter Notebook
In [1]:
import random
def guessing_game():
# Generate a random number between 1 and 100
secret_number = random.randint(1, 100)
# Initialize the user's guess
guess = None
# Keep track of the number of guesses
num_guesses = 0
while guess != secret_number:
try:
guess = int(input("Guess a number between 1 and 100: "))
num_guesses += 1
if guess < secret_number:
print("Too low! Try again.")
elif guess > secret_number:
print("Too high! Try again.")
except ValueError:
print("Invalid input. Please enter a valid number.")
print(f"Congratulations! You've guessed the number {secret_number} in {num_guesses}
# Start the game
guessing_game()
Guess a number between 1 and 100: 55
Too high! Try again.
Guess a number between 1 and 100: 20
Too low! Try again.
Guess a number between 1 and 100: 25
Too high! Try again.
Guess a number between 1 and 100: 22
Congratulations! You've guessed the number 22 in 4 guesses.
localhost:8888/notebooks/Untitled23.ipynb?kernel_name=python3 1/4
8/28/23, 11:42 AM Untitled23 - Jupyter Notebook
In [2]:
import random
def number_guesser(min_range, max_range):
# Generate a random number within the specified range
secret_number = random.randint(min_range, max_range)
# Initialize the user's guess
guess = None
# Keep track of the number of guesses
num_guesses = 0
print(f"Guess a number between {min_range} and {max_range}.")
while guess != secret_number:
try:
guess = int(input("Enter your guess: "))
num_guesses += 1
if guess < secret_number:
print("Too low! Try again.")
elif guess > secret_number:
print("Too high! Try again.")
except ValueError:
print("Invalid input. Please enter a valid number.")
print(f"Congratulations! You've guessed the number {secret_number} in {num_guesses}
# Specify the range for the game
min_range = 1
max_range = 100
# Start the game
number_guesser(min_range, max_range)
Guess a number between 1 and 100.
Enter your guess: 55
Too high! Try again.
Enter your guess: 22
Too low! Try again.
Enter your guess: 26
Too low! Try again.
Enter your guess: 35
Too high! Try again.
Enter your guess: 30
Too high! Try again.
Enter your guess: 28
Congratulations! You've guessed the number 28 in 6 guesses.
localhost:8888/notebooks/Untitled23.ipynb?kernel_name=python3 2/4
8/28/23, 11:42 AM Untitled23 - Jupyter Notebook
In [6]:
import re
def password_strength(password):
# Check length
if len(password) < 8:
return "Weak: Password should be at least 8 characters long."
# Check for uppercase and lowercase letters
if not any(char.isupper() for char in password) or not any(char.islower() for char i
return "Weak: Password should contain both uppercase and lowercase letters."
# Check for digits
if not any(char.isdigit() for char in password):
return "Weak: Password should contain at least one digit."
# Check for special characters using regular expression
special_characters = re.compile(r'[!@#$%^&*(),.?":{}|<>]')
if not special_characters.search(password):
return "Weak: Password should contain at least one special character."
return "Strong: Password meets the required criteria."
# Get password from user
user_password = input("Enter a password: ")
# Evaluate password strength
strength_result = password_strength(user_password)
print(strength_result)
Enter a password: Lavakumar@10
Strong: Password meets the required criteria.
In [7]:
def generate_fibonacci(n):
fibonacci_sequence = [0, 1] # Initialize the sequence with the first two terms
for i in range(2, n):
next_term = fibonacci_sequence[i - 1] + fibonacci_sequence[i - 2]
fibonacci_sequence.append(next_term)
return fibonacci_sequence
# Get the number of terms from the user
num_terms = int(input("Enter the number of terms for the Fibonacci sequence: "))
# Generate and display the Fibonacci sequence
fib_sequence = generate_fibonacci(num_terms)
print(f"The Fibonacci sequence up to {num_terms} terms:")
print(fib_sequence)
Enter the number of terms for the Fibonacci sequence: 5
The Fibonacci sequence up to 5 terms:
[0, 1, 1, 2, 3]
localhost:8888/notebooks/Untitled23.ipynb?kernel_name=python3 3/4
8/28/23, 11:42 AM Untitled23 - Jupyter Notebook
In [9]:
def count_word_occurrences(filename):
word_counts = {} # Dictionary to store word counts
try:
with open(filename, 'r') as file:
for line in file:
words = line.split()
for word in words:
word = word.lower() # Convert to lowercase to treat words case-insen
word = word.strip(".,!?\"'()[]") # Remove common punctuation
if word:
if word in word_counts:
word_counts[word] += 1
else:
word_counts[word] = 1
except FileNotFoundError:
print("File not found.")
return word_counts
# Get the filename from the user
file_name = input("Enter the path of the text file: ")
# Count word occurrences and display the results
word_occurrences = count_word_occurrences(file_name)
sorted_words = sorted(word_occurrences.keys())
print("Word occurrences in alphabetical order:")
for word in sorted_words:
print(f"{word}: {word_occurrences[word]}")
Enter the path of the text file: /content/drive/MyDrive/lava.txt
File not found.
Word occurrences in alphabetical order:
localhost:8888/notebooks/Untitled23.ipynb?kernel_name=python3 4/4