0% found this document useful (0 votes)
4 views4 pages

Python Week10-Practice Session

The document outlines several programming exercises involving loops in Python, including the implementation of the Euclidean algorithm for GCD, collecting weather data, simulating a loan repayment system, creating a countdown timer, calculating the sum of digits in an integer, and developing a simple user authentication system. Each exercise includes code snippets and example outputs to illustrate functionality. The exercises are designed for practice in a computer science course at SPT-Tumakuru.

Uploaded by

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

Python Week10-Practice Session

The document outlines several programming exercises involving loops in Python, including the implementation of the Euclidean algorithm for GCD, collecting weather data, simulating a loan repayment system, creating a countdown timer, calculating the sum of digits in an integer, and developing a simple user authentication system. Each exercise includes code snippets and example outputs to illustrate functionality. The exercises are designed for practice in a computer science course at SPT-Tumakuru.

Uploaded by

shobithlokesh
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Practice session WEEK #10

1. Implement the Euclidean algorithm to find the GCD of two integers using a
while loop.

a = int(input("Enter first number: "))


b = int(input("Enter second number: "))

while b != 0:
r=a%b
a=b
b=r
print("GCD of a and b is:", a)

output
Enter first number: 48
Enter second number: 18
GCD of a and b is: 6

2. Collect daily weather data (e.g., temperature, humidity) from the user. Allow data
entry to continue until the user decides to stop or inputs a sentinel value.

while True: # loop continues until the user decides to stop


temp = float(input("Enter temperature (enter -1 to stop): "))
if temp == -1:
break
humidity = float(input("Enter humidity: "))
print("Recorded Data:")
print("Temperature is:", temp, "Humidity is:", humidity)

output
Enter temperature (enter -1 to stop): 32
Enter humidity: 70
Recorded Data:
Temperature is: 32.0 Humidity is: 70.0
Enter temperature (enter -1 to stop): 30
Enter humidity: 75
Recorded Data:
Temperature is: 30.0 Humidity is: 75.0
Enter temperature (enter -1 to stop): -1

Dept. of CSE, SPT-Tumakuru 1


3. Simulate a loan repayment system where a borrower pays a fixed amount
each month. Continue reducing the loan balance until it is fully paid off, and
display the balance after each payment.

loan = int(input("Enter loan amount: "))


payment = int(input("Enter monthly payment: "))
month = 1
while loan > 0:
loan = loan – payment
if loan < 0:
loan = 0
print("After month", month, "remaining Balance is:", loan)
month = month + 1

output
Enter loan amount: 5000
Enter monthly payment: 1200
After month 1 remaining Balance is: 3800
After month 2 remaining Balance is: 2600
After month 3 remaining Balance is: 1400
After month 4 remaining Balance is: 200
After month 5 remaining Balance is: 0

4. Write a program that simulates a countdown timer. Start with a given number of
seconds and decrement until it reaches zero, displaying the countdown at each step.

seconds = int(input("Enter number of seconds: "))


while seconds >= 0:
print("Time left:", seconds)
seconds = seconds - 1
print("Time is up!")

output
Enter number of seconds: 5
Time left: 5
Time left: 4
Time left: 3
Time left: 2
Time left: 1
Time left: 0
Time is up!

Dept. of CSE, SPT-Tumakuru 2


5. Write a program that takes an integer and calculates the sum of its digits using a
while loop.

num = int(input("Enter an integer: "))


sum = 0
while num > 0:
digit = num % 10
sum = sum + digit
num = num // 10
print("Sum of digits is:", sum)

output
Enter an integer: 456
Sum of digits is: 15

6. Write a program that simulates a simple user authentication system. The program
should:
a. Repeatedly ask the user to input their username and password.
b. Verify the entered credentials against a pre-defined username and password.
c. Allow the user up to three attempts to enter the correct credentials.
d. Provide appropriate feedback for each attempt (e.g., "Incorrect username or
password"). e. If the correct credentials are provided within three attempts,
display a success message and terminate the program.
f. If all three attempts are exhausted, display a failure message and terminate the
program.

username = "admin"
password = "1234"
attempts = 1
while attempts <= 3:
u = input("Enter username: ")
p = input("Enter password: ")
if u = = username and p = = password:
print("Login successful")
break
else:
print("Incorrect username or password")
attempts = attempts + 1
if attempts > 3:
print("Login failed. maximum attempts reached")

Dept. of CSE, SPT-Tumakuru 3


output1
Enter username: admin
Enter password: 1234
Login successful

output2
Enter username: admin
Enter password: 1233
Incorrect username or password
Enter username: admin
Enter password: 1234
Login successful

0utput3
Enter username: admin
Enter password: 1233
Incorrect username or password
Enter username: adminn
Enter password: 1234
Incorrect username or password
Enter username: admins
Enter password: 12345
Incorrect username or password
Login failed. maximum attempts reached

Dept. of CSE, SPT-Tumakuru 4

You might also like