Aim:-Experiment 1:: 18CSC207J-Advance Programming Practice - Structured Programming - Lab Programs
Aim:-Experiment 1:: 18CSC207J-Advance Programming Practice - Structured Programming - Lab Programs
SET-2
Aim:- Implement the Structure Programming in Python
Experiment 1:
Consider you are automate the Graduity calculation using the following a program that reads the
subtotal and the gratuity rate and computes the gratuity and total. For example, if the user enters 10
for the subtotal and 15% for the gratuity rate, the program displays 1.5 as the gratuity and 11.5 as
the total.
Code:
sub_total = int(input('Enter Subtotal:'))
rate = int(input('Enter Rate:'))
gratuity=sub_total * (rate/100)
total=sub_total+gratuity
print("Gratuity:",gratuity)
print("Total:",total)
Snapshots:
Experiment 2:
Write a program that reads an integer between 0 and 1000 and adds all the digits in the integer.
Ensure the Entered number is between the limit if not prompt the user to reenter the number.
Code:
def sumDigits(no):
return 0 if no == 0 else int(no % 10) + sumDigits(int(no / 10))
n = int(input('Enter Number:'))
while(n>=0 or n<0):
if(n<=1000):
print('Sum of Digits:' + str(sumDigits(n)))
break
else:
n = int(input('Please select a number between 0 to 1000.\n Enter Number again:'))
Snapshots:
Experiment 3:
Create a Lottery Application to generate a three-digit lottery number. The program prompts the user
to enter a three-digit number and determines whether the user wins according to the following
rules:
a. If the user input matches the lottery number in the exact order, the award is $10,000.
b. If all the digits in the user input match all the digits in the lottery number, the award is $3,000.
c. If one digit in the user input matches a digit in the lottery number, the award is $1,000.
Code:
print("Enter numbers: ")
maxi = int(input()) #Assign the first number to max
count = 1; # Assign 1 to count
num =maxi # Holds future inputs
#Display to results
print("The largest number is " ,maxi)
print("The occurrence count of the largest number is " , count)
Snapshots:
Experiment 4:
Create a Scissor and Rock simulator that plays the popular scissor-rock paper game. (A scissor can
cut a paper, a rock can knock a scissor, and a paper can wrap a rock.) The program randomly
generates a number 0, 1, or 2 representing scissor, rock, and paper. The program prompts the user
to enter a number 0, 1, or 2 and displays a message indicating whether the user or the computer
wins, loses, or draws.
Code:
from random import randint
computer = randint(0,2)
player = False
player = int(input("0 for Rock.\n1 for Scissors.\n2 for Paper.\n3 to exit.\nEnter your Choice
:"))
if player == computer:
print("Tie!")
player = False
elif player == 0:
player = False
if computer == 2:
print("You lose! Paper covers Rock")
else:
print("You win! Rock smashes Scissors")
elif player == 2:
player = False
if computer == 1:
print("You lose! Scissors cut Paper")
else:
print("You win!Paper covers Rock")
elif player == 1:
player = False
if computer == 0:
print("You lose! Rock smashes Scissors")
else:
print("You win!Scissors cut Paper")
elif player == 3:
player = True
print("Thankyou for playing the game")
else:
print("That's not a valid play. Check your Number!")
player = False
computer = randint(0,2)
Snapshots:
Experiment 5:
Write a program that reads integers, finds the largest of them, and counts its occurrences. Assume
that the input ends with number 0. Suppose that you entered 3 5 2 5 5 5 0; the program finds that
the largest number is 5 and the occurrence count for 5 is 4. (Hint: Maintain two variables, max and
count. The variable max stores the current maximum number, and count stores its occurrences.
Initially, assign the first number to max and 1 to count. Compare each subsequent number with max.
If the number is greater than max, assign it to max and reset count to 1. If the number is equal to
max, increment count by 1.)
Code:
import random
#If the user's guess and generated number are the same they win 10k!
if user == randomNumber:
print("You have won $10,000!")
else:
# Make a list of the matching numbers.
matchNumbers = [x for x in user if x in randomNumber]