exercises

five

Write a program called five.py that tells you if the number you entered is greater than five:

>>> %Run five.py
Enter a number greater than five: 4
False
>>> %Run five.py
Enter a number greater than five: 5
False
>>> %Run five.py
Enter a number greater than five: 34
True

Hint: write a boolean expression that will be True if the number the user types is greater than 5.

Show solution

num = int(input("Enter a number greater than five: "))
print(num > 5)


boolean expressions

Write a program that takes two numbers and gives you the results of three different comparisons:

Enter a number: 4
Enter another number: 5
4 is less than 5: True
4 is equal to 5: False
4 is greater than 5: False


more five

Use an if statement to make five.py more interesting:

>>> %Run five.py
Enter a number greater than five: 6
good job
>>> %Run five.py
Enter a number greater than five: 4
moron

Show solution

num = int(input("Enter a number greater than five: "))
if num > 5:
    print("good job")
else:
    print("moron")


high-low part 2

Make hilo.py tell you if you guessed correctly or not:

>>> %Run hilo.py
Pick a number: 7
Wrong! The computer's number was 64
>>> %Run hilo.py
Pick a number: 64
You got it right!

Show solution

computer_number = 64
guess = int(input("Pick a number: "))
if guess == computer_number:
    print("You got it right!")
else:
    print("Wrong! The computer's number was", computer_number)


previous: if next: the random module