0% found this document useful (0 votes)
26 views6 pages

Book Programs and Challenges of Chapter 5

Uploaded by

Vasu Goyal
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)
26 views6 pages

Book Programs and Challenges of Chapter 5

Uploaded by

Vasu Goyal
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/ 6

#high scores program

scores = []
choice = ''

while choice != '0':


print('''
high scores
0 - exit
1 - add scores
2 - show score
3 - remove scores
4 - sort scores
''')
choice = input('choice: ')
print()

if choice == '0':
print("Goodbye")
elif choice == '1':
score = int(input("Add a score: "))
scores.append(score)
elif choice == '2':
print('High scores:', scores)
elif choice == '3':
remove_score = int(input("Which score to remove: "))
if remove_score in scores:
scores.remove(remove_score)
else:
print("Score is not there")
elif choice == '4':
print("Highest scores:")
scores.sort(reverse=True)
print(scores)
else:
print("Sorry,", choice, "is not valid")

#geek translator program


choice = ''
print('geek translator')
print("""
0-quit
1-look up a geek term
2-add a geek term
3-redefine a geek term
4-delete a geek teem
""")

geek = {"404": "clueless. it work From the web error message 404, meaning page not
found.",
"Googling": "searching the Internet for background information on a
person.",
"Keyboard Plaque" : "the collection of debris found in computer
keyboards.",
"Link Rot": "the process by which web page links become obsolete.",
"Percussive Maintenance": "the act of striking an electronic device to make
",
"Uninstalled": "being fired Especially popular during the dot-bomb era"}

while choice!=0:
choice = int(input("what choice do you wanna select?"))
if choice==0:
print("quit the game")
elif choice==1:
term=input("which term are you looking for:")
if term in geek:
definition=geek[term]
print("the term",term,"means",definition)
else:
print("no such term")
elif choice==2:
term=input("what term would you like to add:")
if term not in geek:
definition=input("whats the definition?")
geek[term]=definition
print(term,"with", definition,"has been added")
else:
print("this term already exists")
elif choice==3:
term=input("what term do you want to redefine?")
if term in geek:
definition=input("whats the new definition")
geek[term]=definition
print("the",term,'has been redefined to', definition)
else:
print("this term doesnt exist")
elif choice==4:
term=input("which term do you want to delete")
if term in geek:
del geek[term]
print("okay i deleted",term)
else:
print("no such term is there")
else:
print("sorry but",choice,"doesnt exist")

#hangman game
import random

words = ('python', 'java', 'laptop', 'cardboard')


word = random.choice(words)
so_far = '-' * len(word)

wrong_guesses = (
"""wrong guess=1""",
"""wrong guess=2""",
"""wrong guess=3""",
"""wrong guess=4""",
"""wrong guess=5""",
"""wrong guess=6""",
"""wrong guess=7"""
)
max_wrong = len(wrong_guesses)
wrong = 0
used = []

print("Welcome to the game")

while max_wrong > wrong and so_far != word:


print(wrong_guesses[wrong])
print("You've used the following letters", used)
print("So far the word is", so_far)

guess = input("Enter your guess: ")

while guess in used:


print("You've already guessed the letter", guess)
guess = input("Enter your guess: ")

used.append(guess)

if guess in word:
new = ''
for i in range(len(word)):
if guess == word[i]:
new += guess
else:
new += so_far[i]
so_far = new
else:
print("Sorry,", guess, "isn't in the word")
wrong += 1

if wrong == max_wrong:
print(wrong_guesses[wrong])
print("You failed")
else:
print("You won")
print("The word was", word)

#challenge1
import random

words = ["vasu", "donna", "anushka", "manas"]


new_list = []

for i in range(4):
word = random.choice(words)
new_list.append(word)
words.remove(word)
print(new_list)

#challenge2
print("What attributes do you think should a person have?")
attributes = {1: 'wisdom', 2: 'dexterity', 3: 'strength', 4: 'health', 5: 'remove'}
print(attributes)

total_points = 0
attribute_points = {1: 0, 2: 0, 3: 0, 4: 0}

while total_points <= 30:


choice = int(input("Enter your choice: "))
if choice == 1:
wisdom = int(input("How much do you think wisdom forms a part of a person's
attribute? "))
if total_points + wisdom <= 30:
total_points += wisdom
attribute_points[1] += wisdom
else:
print("You don't have enough points remaining.")

elif choice == 2:
dexterity = int(input("How much do you think dexterity forms a part of a
person's attribute? "))
if total_points + dexterity <= 30:
total_points += dexterity
attribute_points[2] += dexterity
else:
print("You don't have enough points remaining.")

elif choice == 3:
strength = int(input("How much do you think strength forms a part of a
person's attribute? "))
if total_points + strength <= 30:
total_points += strength
attribute_points[3] += strength
else:
print("You don't have enough points remaining.")

elif choice == 4:
health = int(input("How much do you think health forms a part of a person's
attribute? "))
if total_points + health <= 30:
total_points += health
attribute_points[4] += health
else:
print("You don't have enough points remaining.")

elif choice == 5:
removing = input('Do you want to remove points? (yes or no): ')
if removing.lower() == 'yes':
attribute_to_remove = int(input("Which attribute do you want to remove
points from? (1-4): "))
if attribute_to_remove in attribute_points:
points_to_remove = int(input(f"How many points do you want to
remove from {attributes[attribute_to_remove]}? "))
if points_to_remove <= attribute_points[attribute_to_remove]:
total_points -= points_to_remove
attribute_points[attribute_to_remove] -= points_to_remove
print(f"{points_to_remove} points removed from
{attributes[attribute_to_remove]}")
else:
print("You can't remove more points than you have in that
attribute.")
else:
print("Invalid attribute choice.")
else:
print("No such choice is there.")

print(f"You spent {attribute_points[1]} points on wisdom, {attribute_points[2]} on


dexterity, {attribute_points[3]} on strength, {attribute_points[4]} on health.")

#challenge3
family = {'vasu': 'sandeep', 'swapnil': 'dinesh', 'tamish': 'dalip'}

while True:
name = input("Enter a son's name, and I'll give you their father's name (or
type 'exit' to end): ")

if name == 'exit':
break # Exit the loop if the user types 'exit'

father_name = family.get(name, "Name not found in the dictionary") #comma is


used for default value to be displayed
print("Father's name:", father_name)

choice = int(input("Enter 1 to add, 2 to remove, 3 to redefine a relationship,


and 4 to continue or 5 to exit: "))

if choice == 1:
new_son = input("Enter the name of the son: ")
new_father = input("Enter the name of the father: ")
family[new_son] = new_father
print("Here is the updated list:", family)

elif choice == 2:
remove_son = input("Enter the name of the son to remove: ")
if remove_son in family:
family.pop(remove_son)
print("Here is the updated list:", family)
else:
print(f"{remove_son} not found in the dictionary.")

elif choice == 3:
redefine_son = input("Enter the name of the son to redefine: ")
if redefine_son in family:
new_father = input("Enter the new name of the father: ")
family[redefine_son] = new_father
print("Here is the updated list:", family)
else:
print(f"{redefine_son} not found in the dictionary.")

elif choice == 4:
continue # Continue the loop for the next iteration

elif choice == 5:
print("Program terminated.")
break # Exit the loop and end the program

else:
print("Invalid choice. Please enter a number between 1 and 5.")

You might also like