Climate Quest - Task 5
Objectives
- Understand the role of a
main()
function - Understand the concept of built-in functions in Python.
- Learn how to use some commonly used built-in functions for math, strings, and basic input/output.
- Explore the purpose and usage of functions like
print()
,len()
,input()
, and more.
The main()
function
In many programming languages, there is a designated starting point where the program begins execution. In Python, this convention is followed using a special function often called main()
, along with a specific conditional statement to ensure that this function runs when the script is executed directly.
The main()
function serves as the entry point for the program. It organizes the code and provides a clear structure, making the program easier to understand and maintain.
For example:
Tip
For this task, and any future tasks, do not forget to include this entry point in your code.
Task Overview:
In this homework, you will further develop the "Climate Quest" adventure game by incorporating several built-in functions, organizing the code with a main()
function and writing a function to validate input from the user.
Questions
-
Create the
main()
Function:- Refactor your existing code to include a
main()
function. Move the game initialization and the main gameplay loop into this function.
- Refactor your existing code to include a
-
Enhanced Welcome Message:
- Modify the
main()
function to display a message that includes the length of the player's name entered by the user using thelen()
function.
- Modify the
-
Enhanced Environmental Health Check:
- Update the
ocean_challenge()
andforest_challenge()
functions to display the absolute value of changes in environmental health using theabs()
function.
- Update the
-
Validate User Input:
- Create a
get_valid_input()
function that usesstr.isdigit()
to ensure that numeric inputs are valid. This is used when the user is selecting the location from the menu, and the options presented by each of the challenges.
- Create a
If you're stuck, take a look here
Instructions:
-
Create the
main()
Function:- Introduce the
main()
function as the entry point for the program. - Move the game initialisation and the main gameplay loop into the
main()
function.
- Introduce the
-
Enhanced Welcome Message:
- Use the
len()
function to display the length of the player's name entered by the user.
- Use the
-
Enhanced Environmental Health Check:
- Use the
abs()
function to display the absolute value of changes in environmental health.
- Use the
-
Validate User Input:
- Use the
str.isdigit()
function to ensure that numeric inputs are valid numbers.
- Use the
If you're really stuck ...
Detailed Steps:
-
Create the
main()
Function:Refactor the existing code to include a
main()
function. Themain()
function should initialize the game and handle the main gameplay loop.This code will need the following functions, if not already present in your code.
-
Enhanced Environmental Health Check:
Update the challenge functions to use the
abs()
function when displaying the changes in environmental health. -
Validate User Input:
Create a helper function
get_valid_input()
to ensure numeric inputs are valid. Use this function in the challenge functions.