0% found this document useful (0 votes)
23 views

Python Project

The document describes a Python program that acts as an adventure game. It contains 6 different programs/challenges corresponding to different paths in the game. Players can choose a path by entering a number and experience the associated programming challenge.

Uploaded by

prathijournal
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)
23 views

Python Project

The document describes a Python program that acts as an adventure game. It contains 6 different programs/challenges corresponding to different paths in the game. Players can choose a path by entering a number and experience the associated programming challenge.

Uploaded by

prathijournal
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
You are on page 1/ 13

-: Python Project:-

If elseif ladder

def execute_program(program_choice):
try:
if program_choice == 1:
# Program 1
test1 = float(input("Enter marks for Test 1: "))
test2 = float(input("Enter marks for Test 2: "))
test3 = float(input("Enter marks for Test 3: "))
average1 = (test1 + test2) / 2
average2 = (test2 + test3) / 2
average3 = (test1 + test3) / 2
best_average = max(average1, average2, average3)
print("The best average marks out of the three tests are:", best_average)
elif program_choice == 2:
# Program 2
num = input("Enter a number: ")
if not num.isdigit():
raise ValueError("Invalid input! Please enter a valid number.")
reverse_num = num[::-1]
if num == reverse_num:
print(num, "is a palindrome")
else:
print(num, "is not a palindrome")
digit_count = {}
for digit in num:
if digit in digit_count:
digit_count[digit] += 1
else:
digit_count[digit] = 1
print("Digit counts:")
for digit, count in digit_count.items():
print(digit, ":", count)
elif program_choice == 3:
# Program 3
def fibonacci(n):
if n == 1:
return 0
elif n == 2:
return 1
elif n <= 0:
return "N must be a positive integer."
else:
return fibonacci(n-1) + fibonacci(n-2)

n = int(input("Enter a value for N: "))


result = fibonacci(n)
if isinstance(result, str):
print("Invalid Input-", result)
else:
print("The", n, "th Fibonacci number is:", result)
elif program_choice == 4:
# Program 4
def binary_to_decimal(binary):
decimal = int(binary, 2)
return decimal

def octal_to_hexadecimal(octal):
decimal = int(octal, 8)
hexadecimal = hex(decimal)
return hexadecimal

binary_num = input("Enter a binary number: ")


decimal_num = binary_to_decimal(binary_num)
octal_num = input("Enter an octal number: ")
hexadecimal_num = octal_to_hexadecimal(octal_num)

print("Decimal equivalent of binary", binary_num, "is:", decimal_num)


print("Hexadecimal equivalent of octal", octal_num, "is:", hexadecimal_num)
elif program_choice == 5:
# Program 5
def count_chars(sentence):
word_count = len(sentence.split())
digit_count = sum(1 for char in sentence if char.isdigit())
uppercase_count = sum(1 for char in sentence if char.isupper())
lowercase_count = sum(1 for char in sentence if char.islower())
return word_count, digit_count, uppercase_count, lowercase_count

sentence = input("Enter a sentence: ")


word_count, digit_count, uppercase_count, lowercase_count =
count_chars(sentence)

print("Number of words:", word_count)


print("Number of digits:", digit_count)
print("Number of uppercase letters:", uppercase_count)
print("Number of lowercase letters:", lowercase_count)
elif program_choice == 6:
# Program 6
string1 = input("Enter the first string: ")
string2 = input("Enter the second string: ")
similarity = sum(a == b for a, b in zip(string1, string2)) / max(len(string1), len(string2))
print("Original string:")
print(string1)
print(string2)
print("Similarity between two said strings:")
print(similarity)
else:
print("Invalid program choice. Please enter a number between 1 and 6.")
except ValueError as e:
print(e)

# Get user's program choice


program_choice = int(input("Enter the program number to execute (1-6): "))
execute_program(program_choice)
GAME:-

def test_averages_challenge():
print("Welcome to the Test Averages Challenge!")
try:
test1 = float(input("Enter marks for Test 1: "))
test2 = float(input("Enter marks for Test 2: "))
test3 = float(input("Enter marks for Test 3: "))
average1 = (test1 + test2) / 2
average2 = (test2 + test3) / 2
average3 = (test1 + test3) / 2
best_average = max(average1, average2, average3)
print("The best average marks out of the three tests are:", best_average)
print("Congratulations! You completed the Test Averages Challenge.")
except ValueError:
print("Invalid input. Please enter numeric values for test marks.")

def palindrome_detective():
print("Welcome to the Palindrome Detective!")
try:
num = input("Enter a number: ")
if not num.isdigit():
raise ValueError("Invalid input! Please enter a valid number.")
reverse_num = num[::-1]
if num == reverse_num:
print(num, "is a palindrome")
else:
print(num, "is not a palindrome")
digit_count = {}
for digit in num:
if digit in digit_count:
digit_count[digit] += 1
else:
digit_count[digit] = 1
print("Digit counts:")
for digit, count in digit_count.items():
print(digit, ":", count)
print("Congratulations! You solved the Palindrome Detective mystery.")
except ValueError as e:
print(e)

def fibonacci_quest():
print("Welcome to the Fibonacci Quest!")
def fibonacci(n):
if n == 1:
return 0
elif n == 2:
return 1
elif n <= 0:
return "N must be a positive integer."
else:
return fibonacci(n - 1) + fibonacci(n - 2)

try:
n = int(input("Enter a value for N: "))
result = fibonacci(n)
if isinstance(result, str):
print("Invalid Input -", result)
else:
print("The", n, "th Fibonacci number is:", result)
print("Congratulations! You completed the Fibonacci Quest.")
except ValueError:
print("Invalid input. Please enter a positive integer for N.")

def number_conversion_journey():
print("Welcome to the Number Conversion Journey!")
def binary_to_decimal(binary):
decimal = int(binary, 2)
return decimal

def octal_to_hexadecimal(octal):
decimal = int(octal, 8)
hexadecimal = hex(decimal)
return hexadecimal

try:
binary_num = input("Enter a binary number: ")
decimal_num = binary_to_decimal(binary_num)
octal_num = input("Enter an octal number: ")
hexadecimal_num = octal_to_hexadecimal(octal_num)
print("Decimal equivalent of binary", binary_num, "is:", decimal_num)
print("Hexadecimal equivalent of octal", octal_num, "is:", hexadecimal_num)
print("Congratulations! You completed the Number Conversion Journey.")
except ValueError:
print("Invalid input. Please enter valid binary and octal numbers.")

def char_count_adventure():
print("Welcome to the Char Count Adventure!")
def count_chars(sentence):
word_count = len(sentence.split())
digit_count = sum(1 for char in sentence if char.isdigit())
uppercase_count = sum(1 for char in sentence if char.isupper())
lowercase_count = sum(1 for char in sentence if char.islower())
return word_count, digit_count, uppercase_count, lowercase_count
try:
sentence = input("Enter a sentence: ")
word_count, digit_count, uppercase_count, lowercase_count = count_chars(sentence)
print("Number of words:", word_count)
print("Number of digits:", digit_count)
print("Number of uppercase letters:", uppercase_count)
print("Number of lowercase letters:", lowercase_count)
print("Congratulations! You completed the Char Count Adventure.")
except ValueError:
print("Invalid input. Please enter a sentence.")

def string_similarity_expedition():
print("Welcome to the String Similarity Expedition!")
try:
string1 = input("Enter the first string: ")
string2 = input("Enter the second string: ")
similarity = sum(a == b for a, b in zip(string1, string2)) / max(len(string1), len(string2))
print("Original string:")
print(string1)
print(string2)
print("Similarity between two said strings:")
print(similarity)
print("Congratulations! You completed the String Similarity Expedition.")
except ValueError:
print("Invalid input. Please enter valid strings.")

def play_game():
print("Welcome to the Python Adventure Game!")
print("Choose your path:")
print("1. Test Averages Challenge")
print("2. Palindrome Detective")
print("3. Fibonacci Quest")
print("4. Number Conversion Journey")
print("5. Char Count Adventure")
print("6. String Similarity Expedition")

try:
choice = int(input("Enter the number of your chosen path (1-6): "))

if choice == 1:
test_averages_challenge()
elif choice == 2:
palindrome_detective()
elif choice == 3:
fibonacci_quest()
elif choice == 4:
number_conversion_journey()
elif choice == 5:
char_count_adventure()
elif choice == 6:
string_similarity_expedition()
else:
print("Invalid choice. Your adventure ends here.")
except ValueError:
print("Invalid input. Please enter a number between 1 and 6.")

# Start the game


play_game()
```

Now, each program corresponds to a different path in the game. Players can choose their

🎮
path and experience the challenges associated with each program. Enjoy your Python
Adventure Game!
By,
KEERTHI.M
4MH22CS070
‘A’ SECTION,CSE
MAHARAJA INSTITUTE OF TECHNOLOGY, MYSORE

You might also like