0% found this document useful (0 votes)
11 views

Code and Output

Uploaded by

Siddhant Dhavale
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Code and Output

Uploaded by

Siddhant Dhavale
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

CODE AND OUTPUT

Problem Statement 1:- Design a simple calculator with basic arithmetic operations. Prompt the
user to input two numbers and an operation choice. Perform the calculation and display the result.

#funtion to add two numbers


def ADD(x, y):
return x+y
#function to subtract two numbers
def SUB(x, y):
return x-y
#function to multiply two numbers
def MULT(x, y):
return x*y
#function to divide two numbers
def DIV(x, y):
if y==0:
return "Operaion Error, cannot divide by 0"
else:
return x/y
#selecting operation to perform
print("Select an arithmetic operation to perform:\n"
"1. ADDITION\n" \
"2. SUBTRACTION\n" \
"3. MULTIPLICATION\n" \
"4. DIVISION\n")
#taking inputs from user
while True:
choice=int(input("Select operations to perform:"))
a=float(input("Enter your first number: "))
b=float(input("Enter your second number: "))
if choice==1:
print(a, "+", b, "=" , ADD(a, b))
elif choice==2:
print(a, "-", b, "=", SUB(a, b))
elif choice==3:
print(a, "x", b,"=", MULT(a, b))
elif choice==4:
print(a, "/", b, "=", DIV(a, b))
#if the user want to continue the operations
continue_operation=input("Do you want to continue?(yes/no): ")
if continue_operation=="no":
break
else:
print("Invalid input")
Output:-
Problem Statement 2:- A password generator is a useful tool that generates strong and
random passwords for users. This project aims to create a password generator application
using Python, allowing users to specify the length and complexity of the password. User
Input: Prompt the user to specify the desired length of the password. Generate Password:
Use a combination of random characters to generate a password of the specified length.
Display the Password: Print the generated password on the screen.
#importing random and string
import random
import string
#characters that we are going to use in passowrd
lower=string.ascii_lowercase
upper=string.ascii_uppercase
digits=string.digits
symbols=string.punctuation
def generate_password(length,complexity):
#defining complexity
if complexity=="weak":
all_characters=lower+upper
elif complexity=="medium":
all_characters=lower+upper+digits
elif complexity=="strong":
all_characters=lower+upper+digits+symbols
else:
raise ValueError("Invalid complexity level. Please select from (weak/medium/strong).")
#generating password
password="".join(random.choice(all_characters) for _ in range(length))
return password
#get user input for password length and complexity
while True:
try:
length=int(input("Enter the desired password length: "))
#checking for minimum length of password
if length<6:
print("Minimum 6 characters required. Try again!")
continue #ask for input again
#taking inputs from user
complexity_input=input("Enter the desired complexity of password(weak/medium/strong):
").lower()
password=generate_password(length,complexity_input)
print("Generated Password: ",password)
#break from loop if everything is good
break
except ValueError as a:
print(str(a))
Output:-
Problem Statement 3:- User Input: Prompt the user to choose rock, paper, or scissors.
Computer Selection: Generate a random choice (rock, paper, or scissors) for the computer.
Game Logic: Determine the winner based on the user's choice and the computer's choice.
Rock beats scissors, scissors beat paper, and paper beats rock. Display Result: Show the
user's choice and the computer's choice. Display the result, whether the user wins, loses,
or it's a tie. Score Tracking (Optional): Keep track of the user's and computer's scores for
multiple rounds. Play Again: Ask the user if they want to play another round. User
Interface: Design a user-friendly interface with clear instructions and feedback.
#importing random
import random
#conditions to win the game
def winner(user_choice,computer_choice):
if user_choice==computer_choice:
print("It's a tie!")
elif((user_choice=="paper" and computer_choice=="scissors") or
(user_choice=="rock" and computer_choice=="paper") or
(user_choice=="scissors" and computer_choice=="rock")):
print("Computer wins the game.")
else:
print("You win the game.")

#getting information
def game():
while True:
#user selecting his move
user_choice=input("Enter your move(rock/paper/scissors): ")
print("You chose", user_choice)

#computer selecting its move


computer_choice=random.choice(['rock','paper','scissors'])
print("Computer chose", computer_choice)

#determing the winner


result=winner(user_choice,computer_choice)
print(result)

#asking for another round


play_again=input("Do you want to play again? (yes/no): ").lower()
if play_again =="no":
print ("Thanks for Playing.")
break

if __name__ =="__main__":
game()
Output:-

You might also like