0% found this document useful (0 votes)
3 views12 pages

Python Prg

The document contains multiple Python programs including a number guessing game, a magic 8 ball simulator, a password locker, a wiki markup bullet adder, a quiz generator, and a clipboard manager. Each program serves a different purpose, such as allowing users to guess a number, ask questions, manage passwords, format text, create quizzes, and copy predefined text to the clipboard. The document provides code examples and expected outputs for each program.

Uploaded by

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

Python Prg

The document contains multiple Python programs including a number guessing game, a magic 8 ball simulator, a password locker, a wiki markup bullet adder, a quiz generator, and a clipboard manager. Each program serves a different purpose, such as allowing users to guess a number, ask questions, manage passwords, format text, create quizzes, and copy predefined text to the clipboard. The document provides code examples and expected outputs for each program.

Uploaded by

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

#Guess the Number

import random

def guess(low,hogh):

print(f"\nYou have 7 chances to guess the number between {low} and {high}. Let's start!")

num = random.randint(low, high)

ch = 7 # Total allowed chances

gc = 0 # Guess counter

while gc < ch:

gc += 1

guess = int(input('Enter your guess: '))

if guess == num:

print(f'Correct! The number is {num}. You guessed it in {gc} attempts.')

break

elif gc >= ch and guess != num:

print(f'orry! The number was {num}. Better luck next time.')

elif guess > num:

print('Too high! Try a lower number.')

elif guess < num:

print('Too low! Try a higher number.')

print("Hi! Welcome to the Number Guessing Game.\nYou have 7 chances to guess the number. Let's
start!")

low = int(input("Enter the Lower Bound: "))


high = int(input("Enter the Upper Bound: "))

guess(low,high)

output

Hi! Welcome to the Number Guessing Game.

You have 7 chances to guess the number. Let's start!

Enter the Lower Bound: 5

Enter the Upper Bound: 15

You have 7 chances to guess the number between 5 and 15. Let's start!

Enter your guess: 8

Too high! Try a lower number.

Enter your guess: 6

Correct! The number is 6. You guessed it in 2 attempts.

#Magic 8 Ball with a List


responses = [

"Yes, definitely.",

"As I see it, yes.",

"Reply hazy, try again.",

"Cannot predict now.",

"Do not count on it.",

"My sources say no.",

"Outlook not so good.",

"Very doubtful."

]
while True:

question = input("Ask the magical 8 Ball a question (type 'exit' to quit): ")

if question.lower() == 'exit':

print("Goodbye!")

break

else:

print("Magic 8 Ball says:", random.choice(responses))

output

Ask the magical 8 Ball a question (type 'exit' to quit): will i mood will change 2mrw

Magic 8 Ball says: Do not count on it.

Ask the magical 8 Ball a question (type 'exit' to quit): when will i come out of this pain

Magic 8 Ball says: My sources say no.

Ask the magical 8 Ball a question (type 'exit' to quit): exit

Goodbye!
#Password Locker
password_locker = {}

def add_account(account, username, password):

password_locker[account] = {'username': username, 'password': password}

print(f"Account '{account}' added successfully!")

def view_accounts():

if not password_locker:

print("No accounts saved.")

return

print("\nSaved Accounts:")

for account, creds in password_locker.items():

print(f"Account: {account}, Username: {creds['username']}, Password: {creds['password']}")

def get_password(account):

if account in password_locker:

creds = password_locker[account]

print(f"\nAccount: {account}")

print(f"Username: {creds['username']}")

print(f"Password: {creds['password']}")

else:

print(f"No account found with the name '{account}'.")

while True:

print("\n--- Password Locker ---")

print("1. Add New Account")

print("2. View All Accounts")

print("3. Get Password for an Account")


print("4. Exit")

choice = input("Enter your choice (1-4): ")

if choice == '1':

account = input("Enter account name: ")

username = input("Enter username: ")

password = input("Enter password: ")

add_account(account, username, password)

elif choice == '2':

view_accounts()

elif choice == '3':

account = input("Enter account name to retrieve: ")

get_password(account)

elif choice == '4':

print("Exiting Password Locker.")

break

else:

print("Invalid choice. Please enter a number from 1 to 4.")


output

--- Password Locker ---

1. Add New Account

2. View All Accounts

3. Get Password for an Account

4. Exit

Enter your choice (1-4): 3

Enter account name to retrieve: 555

Account: 555

Username: hhh

Password: yyy

--- Password Locker ---

1. Add New Account

2. View All Accounts

3. Get Password for an Account

4. Exit

Enter your choice (1-4): 4

Exiting Password Locker.


#Adding Bullets to Wiki Markup
def add_bullets_to_wiki(text, bullet_char='*'):

lines = text.strip().split('\n')

bulleted_lines = [f"{bullet_char} {line.strip()}" for line in lines if line.strip()]

return '\n'.join(bulleted_lines)

wiki_text = """

This is the first line

This is the second line

This is the third line

"""

result = add_bullets_to_wiki(wiki_text, bullet_char='*')

print("Modified Wiki Markup:\n")

print(result)

output

Modified Wiki Markup:

* This is the first line

* This is the second line

* This is the third line


#Generating Random Quiz Files
import random

# Simple quiz data (question: correct answer)

quiz_data = {

"Capital of India?": "New Delhi",

"Largest ocean?": "Pacific Ocean",

"2 + 2 = ?": "4",

"Color of the sun?": "Yellow",

"Fastest land animal?": "Cheetah"

# Generate one quiz file and answer key

questions = list(quiz_data.keys())

random.shuffle(questions) # Shuffle question order

# Create quiz file

with open("quiz.txt", "w") as quiz_file, open("answers.txt", "w") as answer_file:

for q_num, question in enumerate(questions, start=1):

correct = quiz_data[question]

options = list(quiz_data.values())

options.remove(correct)

choices = random.sample(options, 3) + [correct]

random.shuffle(choices)

quiz_file.write(f"{q_num}. {question}\n")

for i, opt in enumerate(choices):

quiz_file.write(f" {'ABCD'[i]}. {opt}\n")

quiz_file.write("\n")
correct_letter = 'ABCD'[choices.index(correct)]

answer_file.write(f"{q_num}. {correct_letter}\n")

print("Quiz and answer files generated successfully.")

output

Quiz and answer files generated successfully.

Quiz.txt

1. 2 + 2 = ?

A. 4

B. Cheetah

C. New Delhi

D. Pacific Ocean

2. Largest ocean?

A. New Delhi

B. 4

C. Pacific Ocean

D. Yellow

3. Fastest land animal?

A. Yellow

B. Pacific Ocean

C. 4

D. Cheetah
4. Capital of India?

A. Cheetah

B. Yellow

C. New Delhi

D. 4

5. Color of the sun?

A. Cheetah

B. 4

C. New Delhi

D. Yellow

Answer.txt

1. A

2. C

3. D

4. C

5. D
#Multiclipboard
pip install pyperclip (type this at anaconda command prompt)

import pyperclip

# Simple keyword-to-text mapping

clipboard_data = {

"email": "[email protected]",

"phone": "+91-9876543210",

"address": "123 Main Street, Bengaluru",

"greet": "Hello, how can I help you?"

# Show all available keywords

print("Available keywords:")

for key in clipboard_data:

print(f" - {key}")

# Ask user for keyword

keyword = input("\nEnter a keyword to copy to clipboard: ").strip().lower()

# Copy the corresponding text

if keyword in clipboard_data:

pyperclip.copy(clipboard_data[keyword])

print(f"'{keyword}' copied to clipboard.")

else:

print("Keyword not found.")

output
Available keywords:

- email

- phone

- address

- greet

Enter a keyword to copy to clipboard: phone

'phone' copied to clipboard.

You might also like