# Mark Brown Tuition

Physics, Mathematics and Computer

Science Tuition & Resources

# Short Exercises using simple Python Syntax

Posted on 28-02-18 in Exercises

## Getting Started

For each exercise look at the description carefully and try to implement what it asks. The goal is to match the output exactly. If you get stuck go back through the previous posts.

## Positive or Negative¶

A user enters a number. Tell the user whether this number is positive or negative.

For example :

```
input : -45
output : -45 is a negative number
```

```
value = -45
# ...
```

## Word input¶

A user inputs words until they type stop. Tell the user how many words they have entered and which word is longest and which word is shortest.

For example :

```
input :
Please enter a word >> Cheese
Please enter a word >> marshmellow
Please enter a word >> table
Please enter a word >> hair
Please enter a word >> stop
output :
You have entered 4 words. The longest word is marshmellow. The shortest work is hair.
```

```
# ...
```

## Sum of positive numbers in a list¶

For a given list of numbers, output the sum of numbers but only the positive ones.

For example :

```
input : [-2, 4, -1, 5, 2, -5]
output : The sum of the positive numbers is 11
```

```
list_of_numbers = [-2, 4, -1, 5, 2, -5]
# ...
```

## Sum of Square Numbers¶

Write a program which will add up square numbers from $1^2$ upto a user input $N^2$

For example :

```
input : 4
so it will add 1^2 + 2^2 + 3^2 + 4^2
output : 1 + 4 + 9 + 16 = 30
```

```
value = 4
# ...
```

## Area and perimeter of field¶

A rectangular field with sides *a* and *b* is measured in metres.

- Calculate the area and perimeter of the field. Fencing costs £12 per metre. Each sheep needs 12m2
- Calculate the total cost of fencing and how many sheep the farmer can hold.

For example :

```
input :
a = 10
b = 20
output :
The area of the field is 200m2
The perimeter is 60m
This will cost £720 and can hold 16 sheep.
```

```
a = 10
b = 20
# ...
```

## Guessing game¶

The user has 5 guesses to guess a number between 1 and 100. Tell the user if the answer is larger or smaller than their guess. If the user guesses correctly, tell them they win, otherwise say they lose.

For example :

```
Guess Number 1
Enter a number : 2
Incorrect! Answer is bigger than this
Guess Number 2
Enter a number : 67
Incorrect! Answer is bigger than this
Guess Number 3
Enter a number : 88
Incorrect! Answer is smaller than this
Guess Number 4
Enter a number : 75
Correct! You win!
```

```
from random import randint
answer = randint(1, 100)
# ...
```

## Number of Divisors¶

Using the modulus operator determine how many divisors a number has.

For example :

```
input : 25
output : 25 is divisible by 1, 5, 25. There are 3 divisors.
```

```
value = 25
# ...
```