os,time Library
What is the os library?It allows us to "talk" to the console. One of the most powerful things we can do with this library is allow it to clear the console.
The os library in Python provides a way of using operating system dependent functionality. It allows you to interact with the operating system in various ways, such as navigating file systems, accessing environment variables, and executing system commands. Here are some common functionalities provided by the os module:
File and Directory Operations: You can perform operations related to file and directory manipulation, such as creating, deleting, renaming, and checking existence of files and directories.The os library in Python provides a way of using operating system dependent functionality. It allows you to interact with the operating system in various ways, such as navigating file systems, accessing environment variables, and executing system commands. Here are some common functionalities provided by the os module:
Operating System Information: You can retrieve information about the operating system, such as the name, environment variables, and working directory.
Process Management: You can interact with processes, such as launching new processes, terminating processes, and obtaining process IDs.
Path Manipulation: The os.path submodule provides functions for manipulating file paths in a platform-independent way, including joining paths, splitting paths, and getting the basename and directory name of a path.
Permissions and Ownership: You can set and retrieve file permissions and ownership information.
System Utilities: The os module also includes various system-related utilities, such as functions for executing shell commands, handling signals, and working with environment variables.
import os
print("Welcome")
print("to")
print("Python")
os.system("clear")
username = input("Username: ")
We can import a second library by placing a , after the name of the first library.
import os, time
This library allows us to pause the execution of a program for a specific amount of time.
The time.sleep(1) function allows us to pause the program for the amount of seconds listed in the ().
time.sleep(1)
os.system("clear")
Example - A simple example that demonstrates using the os and time libraries to create a program that displays the current date and time:
import os
import time
# Clear the console screen (for demonstration purposes)
os.system('cls' if os.name == 'nt' else 'clear')
# Get the current date and time
current_time = time.strftime("%Y-%m-%d %H:%M:%S")
# Display the current date and time
print("Current date and time:", current_time)
We use the time.strftime() function to format the current date and time according to the specified format string "%Y-%m-%d %H:%M:%S". This format represents the year, month, day, hour, minute, and second in a specific order.
Example -
Play a song from this repl and build a menu to go with it. Make it look like an iPod!Use a while true loop to create a title for a music player.Allow the user to select to play a song and use subroutine called 'play' when they select the song.
Give the user the option to exit the program.
The title should pop up and pause along with the menu options.
If the user chooses anything else, start again by clearing the screen.
from replit import audio
import os, time
def play():
source = audio.play_file('audio.wav')
source.paused = False # unpause the playback
while True:
stop_playback = int(input("Press 2 anytime to stop playback and go back to the menu : ")) # giving the user the option to stop playback
if stop_playback == 2:
source.paused = True # let's pause the file
return # let's go back from this play() subroutine
else:
continue
while True:
os.system("clear")
print("🎵 MyPOD Music Player ")
time.sleep(1)
print("Press 1 to Play")
time.sleep(1)
print("Press 2 to Exit")
time.sleep(1)
print("Press anything else to see the menu again")
userInput = int(input())
if userInput == 1:
print("Playing some proper tunes!")
play()
elif userInput == 2:
exit()
else :
continue
Example - You will create a video game that creates a character's health and attack stats...along with an epic name for your character.
Write a subroutine that generates a character: first name and character type (human, imp, wizard, elf, etc.).
Write a subroutine that multiplies a bunch of random dice rolls together to generate the character's health stats. Use this formula: (six_sided_roll*twelve_sided_roll)/2)+10
Write a second subroutine that multiplies a bunch of random dice rolls together to generate the character's strength stats. Use this formula:(six_sided_roll*twelve_sided_roll)/2)+12Write a subroutine that generates a character: first name and character type (human, imp, wizard, elf, etc.).
Write a subroutine that multiplies a bunch of random dice rolls together to generate the character's health stats. Use this formula: (six_sided_roll*twelve_sided_roll)/2)+10
Present the data in a menu with time.sleep and os.system("clear") to make it appealing.
Wrap it in a loop so the user has the option to create another character.
Wrap it in a loop so the user has the option to create another character.
import os,time
def character_health(charater_name):
import random
six_sided_roll= random.randint(1,6)
twelve_sided_roll= random.randint(1,12)
health_stat= ((six_sided_roll*twelve_sided_roll)/2)+10
return(health_stat)
def character_strenth(charater_name):
import random
six_sided_roll= random.randint(1,6)
twelve_sided_roll= random.randint(1,12)
strength_stat= ((six_sided_roll*twelve_sided_roll)/2)+10
return(strength_stat)
while True:
os.system("clear")
print("Character Builder")
print("")
character_name = input("Name of your legend:")
character_type = input("Character Type(Human,Elf,Wizard,Orc):")
print()
print(character_name)
print("Health:",character_health(character_name))
print("Strength:",character_strenth(character_name))
print()
print("May your name go down in legend")
print()
again_no= input("want it (yes/no):")
if again_no == "yes":
continue
else:
break
No comments:
Post a Comment