Fundamentals of Computational Intelligence
(COMP1202) - 2023 Laboratory Week 6 -
Problems
Task 1.
Write a program that asks the user for two integers and prints whichever number is larger.
If both numbers are the same, instead tell the user they have made a mistake.
For example, the program output might look as follows, when the user inputs their two
numbers as 56 and 23:
Please enter the first integer 56
Please enter the second integer 23
56
Task 2.
The following statements generate a random integer between 1 and 100 (inclusive) and
stores it in a variable called “random_number”:
import random
random_number = [Link](1, 100)
Using this, write a program that generates a random number between 1 and 100, and then
asks the user to guess what the number is. If the user's guess is higher than the random
number, the program should print "Too high, try again." If the user's guess is lower
than the random number, the program should print "Too low, try again." The user
can keep guessing until they get the correct number, after which the program should print
how many guesses it took them.
For example, if the randomly generated number was equal to 16, the output might look as
follows:
Guess the number 45
Too high, try again.
Guess the number 23
Too high, try again.
Guess the number 7
Too low, try again.
Guess the number 16
Number of guesses: 4
Task 3.
Write a function called “shape_area” that has three parameters called “shape”, “width”, and
“height”.
The specifications for this function are as follows:
- If the shape parameter is equal to the string “Rectangle”, the function should print the
width parameter multiplied by the height parameter.
- If the shape parameter is equal to the string “Square”, the function should print the
width parameter multiplied by itself.
- If the shape parameter is equal to the string “Circle”, the function should print the
result of the following steps:
o Divide the width parameter by two.
o Multiply this result by itself.
o Multiply this result by 3.14
- If the shape parameter is any other value, print "Shape not supported".
For example, calling this function with the following statement:
shape_area("Rectangle", 100, 50)
shape_area("Square", 70, 0)
shape_area("Circle", 50, 0)
shape_area("Hexagon", 20, 0)
Should print the following:
5000
4900
1962.5
Shape not supported
Task 4.
The Fibonacci sequence is a famous sequence of numbers, and is defined as follows:
- The first two numbers in the sequence are 1 and 1.
- Every other number in the sequence is the sum of the two preceding numbers.
So, the first 15 numbers in the Fibonacci sequence are:
- 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 …
Write a function called “fibonacci_sequence”, that has a single parameter called “n”. This
function should print the nth number in the Fibonacci sequence. For example, calling this
function with the following statement:
fibonacci_sequence(100)
Should print the 100th number in the Fibonacci sequence:
354224848179261915075
Task 5.
Once Tasks 3 and 4 are completed, save both the “shape_area” and “fibonacci_sequence”
functions to a single Python file called “[Link]”. Submit this file to CodeGrade for automatic
marking. You can find the link under the “Material week 6” module on Canvas.
Make sure to check that CodeGrade awards you full marks. Codegrade will be used to check
your Python assignment, so it is very important that you know how to submit code to it.