import json
import os
import datetime
def export_results(results, filename="results.json"):
"""Exports results to a JSON file."""
try:
with open(filename, 'w') as f:
json.dump(results, f, indent=4) # Use indent for readability
print(f"Results exported to {filename}")
except Exception as e:
print(f"Error exporting results: {e}")
def get_user_input(prompt, input_type=str):
"""Gets user input with specified type, handling older Python versions."""
while True:
try:
user_input = input(prompt)
if input_type == int:
return int(user_input)
elif input_type == float:
return float(user_input)
else:
return user_input
except ValueError:
print("Invalid input. Please try again.")
def main():
"""Main function to gather input and export results."""
# Get user input
name = get_user_input("Enter your name: ")
age = get_user_input("Enter your age: ", int)
city = get_user_input("Enter your city: ")
score = get_user_input("Enter your score: ", float)
# Create results dictionary
results = {
"name": name,
"age": age,
"city": city,
"score": score,
"timestamp": datetime.datetime.now().isoformat() # Add timestamp
}
# Export results
export_results(results)
if __name__ == "__main__":
main()
Add your comment