0% found this document useful (0 votes)
11 views5 pages

Codsoft Assignment

COD SOFT ASSIGNMENT

Uploaded by

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

Codsoft Assignment

COD SOFT ASSIGNMENT

Uploaded by

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

TASK 1

CALCULATOR
def add(x, y):
return x + y

def subtract(x, y):


return x - y

def multiply(x, y):


return x * y

def divide(x, y):


if y == 0:
return
else:
return x / y

def get_operation_choice():
print("Select operation:")
print("1. Add")
print("2. Subtract")
print("3. Multiply")
print("4. Divide")

while True:
choice = input("Enter choice (1/2/3/4): ")
if choice in ('1', '2', '3', '4'):
if choice == '1':
return add
elif choice == '2':
return subtract
elif choice == '3':
return multiply
else:
return divide
else:
print("Invalid input. Please enter 1, 2, 3 or 4.")

def main():
print("Simple Calculator")

while True:
try:
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
operation = get_operation_choice()
result = operation(num1, num2)
print("Result:", result)
break
except ValueError:
print("Invalid input. Please enter numbers only.")

if __name__ == "__main__":
main()

Task 2
PASSWORD GENERATOR

import random

def generate_password(length, lowercase, uppercase, digits,


punctuation):

characters = []
if lowercase:
characters.extend(string.ascii_lowercase)
if uppercase:
characters.extend(string.ascii_uppercase)
if digits:
characters.extend(string.digits)
if punctuation:
characters.extend(string.punctuation)

# Ensure at least one character from each included category


if not characters:
raise ValueError("Please select at least one character type
(lowercase, uppercase, digits, punctuation).")

if len(characters) < length:


# Add extra characters from the included categories to meet desired
length
for category in [lowercase, uppercase, digits, punctuation]:
if category:
characters.extend(random.sample(getattr(string,
category.upper()), length - len(characters)))
break

password = ''.join(random.sample(characters, length))


return password

def main():
print("Welcome to the Password Generator!")

while True:
try:
length = int(input("Enter desired password length (minimum 8
characters): "))
if length < 8:
raise ValueError("Password length must be at least 8
characters.")

lowercase = input("Include lowercase letters (y/n): ").lower() ==


'y'
uppercase = input("Include uppercase letters (y/n): ").lower() ==
'y'
digits = input("Include digits (y/n): ").lower() == 'y'
punctuation = input("Include punctuation characters (y/n):
").lower() == 'y'

password = generate_password(length, lowercase, uppercase,


digits, punctuation)
print("Your generated password:", password)
break
except ValueError as e:
print("Invalid input:", e)

if __name__ == "__main__":
main()

Task 3
Rock-Paper-Scissors Game
import random

def get_user_choice():

while True:
choice = input("Enter your choice (rock, paper, scissors):
").lower()
if choice in ('rock', 'paper', 'scissors'):
return choice
else:
print("Invalid choice. Please enter rock, paper, or scissors.")

def get_computer_choice():
choices = ['rock', 'paper', 'scissors']
return random.choice(choices)
def determine_winner(user_choice, computer_choice):

if user_choice == computer_choice:
return "tie"
elif user_choice == 'rock':
if computer_choice == 'scissors':
return "user"
else:
return "computer"
elif user_choice == 'paper':
if computer_choice == 'rock':
return "user"
else:
return "computer"
else:
if computer_choice == 'paper':
return "user"
else:
return "computer"

def play_game():
user_choice = get_user_choice()
computer_choice = get_computer_choice()
winner = determine_winner(user_choice, computer_choice)

print(f"You chose {user_choice}. Computer chose {computer_choice}.")

if winner == "user":
print("You win!")
elif winner == "computer":
print("You lose.")
else:
print("It's a tie!")

return winner

def main():
user_score = 0
computer_score = 0
play_again = 'y'

while play_again.lower() == 'y':


winner = play_game()
if winner == "user":
user_score += 1
elif winner == "computer":
computer_score += 1
print(f"Current score: User - {user_score}, Computer -
{computer_score}")

play_again = input("Play again? (y/n): ")

print("Thanks for playing!")

if __name__ == "__main__":
main()

You might also like