You are on page 1of 3

Name : Arjun Singh

Enrolment No. : A12405218004

Class : 5CSE13X

Q. Make a simple mathematical calculator which can perform addition, subtraction, multiplication
and division?

Code :

# This function adds two numbers


def add(x, y):
return x + y
# This function subtracts two numbers
def subtract(x, y):
return x - y
# This function multiplies two numbers
def multiply(x, y):
return x * y
# This function divides two numbers
def divide(x, y):
return x / y
print("Select operation.")
print("1.Add")
print("2.Subtract")
print("3.Multiply")
print("4.Divide")
while True:
# Take input from the user
choice = input("Enter choice(1 2 3 4): ")
# Check if choice is one of the four options
if choice in ('1', '2', '3', '4'):
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
if choice == '1':
print(num1, "+", num2, "=", add(num1, num2))
elif choice == '2':
print(num1, "-", num2, "=", subtract(num1, num2))
elif choice == '3':
print(num1, "*", num2, "=", multiply(num1, num2))
elif choice == '4':
print(num1, "/", num2, "=", divide(num1, num2))
break
else:
print("Invalid Input")
Output :

Q. Make a rock-paper-scissors game where it is the player vs the computer. The computer’s answers
will be randomly generated, while the program will ask the input from the user. Use of loops and if
statement.

Code :

import random
print("Rules of the Rock paper scissor game as follows: \n"+"Rock vs paper-
>paper wins\n"+ "Rock vs scissor->Rock wins\n"+"paper vs scissor->scissor wins
\n")
while True:
print("Enter choice \n 1. Rock \n 2. paper \n 3. scissor \n")
choice = int(input("User turn: "))
while choice > 3 or choice < 1:
choice = int(input("enter valid input: "))
if choice == 1:
choice_name = 'Rock'
elif choice == 2:
choice_name = 'paper'
else:
choice_name = 'scissor'
print("user choice is: " + choice_name)
print("\nNow its computer turn.......")
comp_choice = random.randint(1, 3)
while comp_choice == choice:
comp_choice = random.randint(1, 3)
if comp_choice == 1:
comp_choice_name = 'Rock'
elif comp_choice == 2:
comp_choice_name = 'paper'
else:
comp_choice_name = 'scissor'
print("Computer choice is: " + comp_choice_name)
print(choice_name + " V/s " + comp_choice_name)
if((choice == 1 and comp_choice == 2) or
(choice == 2 and comp_choice ==1 )):
print("paper wins => ", end = "")
result = "paper"
elif((choice == 1 and comp_choice == 3) or
(choice == 3 and comp_choice == 1)):
print("Rock wins =>", end = "")
result = "Rock"
else:
print("scissor wins =>", end = "")
result = "scissor"
if result == choice_name:
print("<== User wins ==>")
else:
print("<== Computer wins ==>")
print("Do you want to play again? (Y/N)")
ans = input()
if ans == 'n' or ans == 'N':
print("\nThanks for playing the game ")

Output:

You might also like