Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
22 views

Python Output

python

Uploaded by

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

Python Output

python

Uploaded by

rajahr.mba07
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

1.

Create a code generator


def generate_factorial_function():
"""
Generate a Python function that calculates the factorial of a number.
"""
function_name = input("Enter a name for the function: ")
argument_name = input("Enter a name for the argument: ")
code = f"def {function_name}({argument_name}):\n"
code += f" if {argument_name} <= 1:\n"
code += f" return 1\n"
code += f" else:\n"
code += f" return {argument_name} * {function_name}({argument_name}-1)"
print(code)

2.Build a countdown calculator


import time

def countdown(t):
while t:
mins, secs = divmod(t, 60)
timer = '{:02d}:{:02d}'.format(mins, secs)
print(timer, end="\r")
time.sleep(1)
t -= 1
print('Time is up!')

t = input("Enter the time in seconds: ")

countdown(int(t))

3.Write a sorting method


def bubble_sort(arr):
n = len(arr)
for i in range(n):
# Last i elements are already sorted
for j in range(0, n-i-1):
# Swap if the element found is greater than the next element
if arr[j] > arr[j+1] :
arr[j], arr[j+1] = arr[j+1], arr[j]
return arr

6. Make a temperature/measurement converter


def convert_temperature(temp, unit_in, unit_out):
if unit_in == "C" and unit_out == "F":
return (temp * 1.8) + 32
elif unit_in == "F" and unit_out == "C":
return (temp - 32) * 0.5556
elif unit_in == "C" and unit_out == "K":
return temp + 273.15
elif unit_in == "K" and unit_out == "C":
return temp - 273.15
elif unit_in == "F" and unit_out == "K":
return (temp + 459.67) * 0.5556
elif unit_in == "K" and unit_out == "F":
return (temp * 1.8) - 459.67
else:
return temp

def convert_measurement(val, unit_in, unit_out):


if unit_in == "m" and unit_out == "km":
return val / 1000
elif unit_in == "km" and unit_out == "m":
return val * 1000
elif unit_in == "m" and unit_out == "ft":
return val * 3.28084
elif unit_in == "ft" and unit_out == "m":
return val / 3.28084
elif unit_in == "cm" and unit_out == "in":
return val / 2.54
elif unit_in == "in" and unit_out == "cm":
return val * 2.54
else:
return val

print(convert_temperature(100, "C", "F")) # should print 212.0


print(convert_temperature(212, "F", "C")) # should print 100.0
print(convert_temperature(0, "C", "K")) # should print 273.15
print(convert_temperature(273.15, "K", "C")) # should print 0.0
print(convert_temperature(-40, "F", "K")) # should print 233.15
print(convert_temperature(233.15, "K", "F")) # should print -40.0

print(convert_measurement(1000, "m", "km")) # should print 1.0


print(convert_measurement(1, "km", "m")) # should print 1000.0
print(convert_measurement(1000, "m", "ft")) # should print 3280.84
print(convert_measurement(3280.84, "ft", "m")) # should print 1000.0
print(convert_measurement(10, "cm", "in")) # should print 3.937007874015748
print(convert_measurement(3.937007874015748, "in", "cm")) # should print 10.0

7. Build a counter app

# Define a counter variable


counter = 0

# Loop until the user chooses to quit


while True:
# Print the current count
print(f"Count: {counter}")

# Ask the user what they want to do next


choice = input("What would you like to do? (q to quit) ")

# Check if the user wants to quit


if choice == "q":
break

# Increment or decrement the counter based on user input


if choice == "+":
counter += 1
elif choice == "-":
counter -= 1
else:
print("Invalid choice. Please try again.")

8. Build a number-guessing game

import random

def play_game():
# Generate a random number between 1 and 100
number = random.randint(1, 100)
guesses = 0

# Loop until the player guesses the correct number


while True:
# Get the player's guess
guess = input("Guess a number between 1 and 100: ")
guesses += 1

# Convert the player's guess to an integer


try:
guess = int(guess)
except ValueError:
print("Invalid input. Please enter a number.")
continue

# Check if the player's guess is correct


if guess == number:
print("Congratulations, you guessed the number in", guesses,
"guesses!")
break
elif guess < number:
print("The number is higher. Try again.")
else:
print("The number is lower. Try again.")

# Call the play_game function to start the game


play_game()

9. Build an alarm clock

import datetime
import time
import winsound

def set_alarm(alarm_time):
while True:
time_now = datetime.datetime.now().strftime("%H:%M:%S")
if time_now == alarm_time:
print("Time to wake up!")
winsound.PlaySound("sound.wav", winsound.SND_ASYNC)
break

alarm_time = input("Enter the time for the alarm in HH:MM:SS format: ")
set_alarm(alarm_time)

You might also like