100% found this document useful (1 vote)
170 views

Aim:-Experiment 1:: 18CSC207J-Advance Programming Practice - Structured Programming - Lab Programs

The document contains code for 5 programming experiments involving structured programming in Python. Experiment 1 calculates gratuity and total from a subtotal and gratuity rate input by the user. Experiment 2 sums the digits of a number input by the user between 0-1000. Experiment 3 generates a random 3-digit number and awards the user for matching digits. Experiment 4 simulates a rock-paper-scissors game against the computer. Experiment 5 finds the largest number and count of its occurrences from integers input by the user.

Uploaded by

PRJ
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
170 views

Aim:-Experiment 1:: 18CSC207J-Advance Programming Practice - Structured Programming - Lab Programs

The document contains code for 5 programming experiments involving structured programming in Python. Experiment 1 calculates gratuity and total from a subtotal and gratuity rate input by the user. Experiment 2 sums the digits of a number input by the user between 0-1000. Experiment 3 generates a random 3-digit number and awards the user for matching digits. Experiment 4 simulates a rock-paper-scissors game against the computer. Experiment 5 finds the largest number and count of its occurrences from integers input by the user.

Uploaded by

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

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

#Assume that the input ends with number 0


while (num > 0):
num = int(input())
if (num > maxi):
maxi = num
count = 1
elif(num == maxi):
count=count+1

#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

while 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

#Gets user's guess.


user = str(input("Enter a three digit number: "))

#Generate a random three digit number.


randomNumber = str(random.randrange(100,1000))

print("The lottery number is " + randomNumber)

#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]

# All numbers match, just not in the right order.


if len(matchNumbers) == 3:
print("You have won $3,000!")

# Will return True if at least one number matched.


elif matchNumbers:
print("You have won $1,000!")

# Nothing matched, they get nothing.


else:
print("You Lose!")
Snapshots:

You might also like