Professional Documents
Culture Documents
import random
# generates random integer number between 1 and 100
x = random.randint(1,100)
print(x)
Functions:
• Define a function absolute(n) that returns the absolute value of n.
• Define a function get_location() that generates a random integer number
between 1 and 10, inclusive, and returns it.
• Define a function get_distance(x, y, guess_x, guess_y) that takes four
parameters: x, y, guess_x and guess_y. The function calculates the Manhattan
distance between two points: (x, y) and (guess_x, guess_y) by using the
absolute() function. The function returns the calculated distance.
𝑀𝑎𝑛ℎ𝑎𝑡𝑡𝑎𝑛 𝑑𝑖𝑠𝑡𝑎𝑛𝑐𝑒 = |𝑥1 − 𝑥2 | + |𝑦1 − 𝑦2 |
Instructions:
• Generate the location of the treasure by calling the get_location() function for x and y
coordinates.
• Write a while loop that continues until number of guesses is less than 10. During each
iteration of the loop, do the following:
o Ask the user to enter their guess for the x and y coordinates of the treasure.
o Compute the Manhattan distance between the user's guess and the true
location of the treasure by calling the get_distance() function and passing in
the appropriate parameters.
o Provide advice to the user based on the calculated distance by calling the
get_advice() function and passing in the calculated distance.
o If the user has not found the treasure, print the remaining number of guesses.
o If the user has found the treasure, end the game by breaking out of the while
loop.
• If the user fails to find the treasure within 10 guesses, print a message that reveals the
true location of the treasure.
Make sure that after the user has correctly found the location, the execution of the loop-body
will skip even if there are more rounds remaining.
Sample Output:
Sorry, you did not find the treasure. The true location was 8 , 1
Page 4 of 4
Notes:
• The program should use proper indentation and include comments to make the code
more readable and understandable.
• Late submissions will not be allowed!
• All work on homeworks must be done individually.
Submit:
• You must submit your homeworks via the online learning system. (Homeworks sent by
e-mail will not be accepted.)
• Name your file as HW_homeworkNo_StudentId_Name_Surname.ipynb or
HW_homeworkNo_StudentId_Name_Surname.py
Colab >> File >> Download >> Choose [Download .ipynb] or [Download .py] as shown
the following:
Lack of proper application of the above guidelines may result loss of points.