This commit is contained in:
Casper 2024-12-05 13:36:59 +01:00
commit 552488d4c6
8 changed files with 158 additions and 0 deletions

3
.idea/.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
# Default ignored files
/shelf/
/workspace.xml

View File

@ -0,0 +1,21 @@
<component name="InspectionProjectProfileManager">
<profile version="1.0">
<option name="myName" value="Project Default" />
<inspection_tool class="HtmlUnknownTag" enabled="true" level="WARNING" enabled_by_default="true">
<option name="myValues">
<value>
<list size="7">
<item index="0" class="java.lang.String" itemvalue="nobr" />
<item index="1" class="java.lang.String" itemvalue="noembed" />
<item index="2" class="java.lang.String" itemvalue="comment" />
<item index="3" class="java.lang.String" itemvalue="noscript" />
<item index="4" class="java.lang.String" itemvalue="embed" />
<item index="5" class="java.lang.String" itemvalue="script" />
<item index="6" class="java.lang.String" itemvalue="turbo-frame" />
</list>
</value>
</option>
<option name="myCustomValuesEnabled" value="true" />
</inspection_tool>
</profile>
</component>

View File

@ -0,0 +1,6 @@
<component name="InspectionProjectProfileManager">
<settings>
<option name="USE_PROJECT_PROFILE" value="false" />
<version value="1.0" />
</settings>
</component>

7
.idea/misc.xml Normal file
View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Black">
<option name="sdkName" value="Python 3.9 (pythonProject)" />
</component>
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.9 (pythonProject)" project-jdk-type="Python SDK" />
</project>

8
.idea/modules.xml Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/pythonProject.iml" filepath="$PROJECT_DIR$/.idea/pythonProject.iml" />
</modules>
</component>
</project>

10
.idea/pythonProject.iml Normal file
View File

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/.venv" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

6
.idea/vcs.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

97
main.py Normal file
View File

@ -0,0 +1,97 @@
import random
from colorama import Fore, Style
# Unit hierarchy and conversion factors
unit_hierarchy = ["km", "hm", "dam", "m", "dm", "cm", "mm"]
conversion_factors = [10, 10, 10, 10, 10, 10] # Stepwise conversion factor
# Generate a random conversion question
def generate_question(max_jump=2):
from_index = random.randint(0, len(unit_hierarchy) - 2)
to_index = random.randint(max(0, from_index - max_jump),
min(len(unit_hierarchy) - 1, from_index + max_jump))
# if from or to index is 2, get another one
if from_index == 2:
from_index += 1
if to_index == 2:
to_index += 1
if from_index == to_index:
# add one to to_index if it is not the last index and new index not 2
if to_index < len(unit_hierarchy) - 1 and to_index != 1:
to_index += 1
else:
to_index -= 1
# Ensure meaningful values for the question
if from_index < to_index: # Larger to smaller unit
number = random.randint(1, 100)
else: # Smaller to larger unit
number = random.randint(10, 1000)
from_unit = unit_hierarchy[from_index]
to_unit = unit_hierarchy[to_index]
correct_answer = calculate_conversion(number, from_unit, to_unit)
return f"{number} {from_unit} equals x {to_unit}", correct_answer
# Calculate conversion between two units
def calculate_conversion(number, from_unit, to_unit):
from_index = unit_hierarchy.index(from_unit)
to_index = unit_hierarchy.index(to_unit)
factor = 1
if from_index < to_index: # Larger to smaller
for i in range(from_index, to_index):
factor *= conversion_factors[i]
else: # Smaller to larger
for i in range(to_index, from_index):
factor *= conversion_factors[i]
factor = 1 / factor # Reciprocal for smaller to larger
return round(number * factor, 3)
def main():
print("Welcome to the Unit Conversion Quiz!")
try:
num_questions = int(input("How many questions would you like? "))
except ValueError:
print(Fore.RED + "Please enter a valid number." + Style.RESET_ALL)
return
correct_count = 0
for i in range(num_questions):
question, correct_answer = generate_question()
print(f"\nQuestion {i + 1}/{num_questions}: {question}")
attempt = 0
while attempt < 2: # Allow up to two attempts
try:
user_answer = float(input("Your answer: "))
if abs(user_answer - correct_answer) < 1e-5: # Handle floating-point precision
print(Fore.GREEN + "Correct!" + Style.RESET_ALL)
correct_count += 1
break
else:
attempt += 1
if attempt < 2:
print(Fore.YELLOW + "Wrong! Try again." + Style.RESET_ALL)
else:
print(Fore.RED + f"Wrong! The correct answer is {correct_answer}" + Style.RESET_ALL)
except ValueError:
print(Fore.RED + "Invalid input. Please enter a number." + Style.RESET_ALL)
attempt += 1
if attempt >= 2:
print(Fore.RED + f"The correct answer was {correct_answer}" + Style.RESET_ALL)
print(f"\nQuiz completed! You answered {correct_count}/{num_questions} correctly.")
if correct_count == num_questions:
print(Fore.GREEN + "Perfect score! Great job!" + Style.RESET_ALL)
elif correct_count > num_questions // 2:
print(Fore.YELLOW + "Good effort!" + Style.RESET_ALL)
else:
print(Fore.RED + "Better luck next time!" + Style.RESET_ALL)
if __name__ == "__main__":
main()