Inducción al uso de Python

Práctica 5: Ciclos while

Objective

During this activity, students should be able to:


Activity Description

Solve the following set of problems using Python 3. Run and test each of your programs to make sure they work as expected.

All the following problems must be solved using while loops.

  1. Write a program called numbers.py. This program should request the user to input an arbitrary amount of integer numbers. The program should stop requesting more input when the user types a zero. The program should then print how many numbers were typed, and the sum and average of all those numbers (without considering the zero). You do not need to define any functions other than main().

    Example (user input is in blue):

    Input an integer number (0 to quit): 16
    Input an integer number (0 to quit): 4
    Input an integer number (0 to quit): 42
    Input an integer number (0 to quit): 8
    Input an integer number (0 to quit): 23
    Input an integer number (0 to quit): 15
    Input an integer number (0 to quit): 0
    6 numbers were typed.
    The sum of all those numbers is: 108
    The average of all those number is: 18.0
    
  2. Write a program called guess.py. The program should pick a random integer number \(n\) from 1 to 100. It should then allow the user to guess this number. If the user’s guess is greater or less than \(n\) the program should display a message saying so, and then request the user for a new guess; but if the user’s guess is equal to \(n\), the program should end displaying how many tries it took the user to guess correctly. You do not need to define any functions other than main().

    Example (user input is in blue):

    I'm thinking of a number between 1 and 100.
    Try to guess what number it is.
    What's your guess? 50
    Your guess is too low.
    What's your guess? 75
    Your guess is too low.
    What's your guess? 90
    Your guess is too high.
    What's your guess? 85
    Your guess is too low.
    What's your guess? 87
    Your guess is too low.
    What's your guess? 88
    Correct!
    It only took you 6 tries!
    

    The randint(a, b) function, from the random module, returns a random integer in range [a, b], including both end points. For example, the following program prints a random number from 1 to 10:

    from random import randint
    
    def main():
        r = randint(1, 10)
        print(r)
    
    main()
  3. Write a program called invert.py. Define in this program a function called invert(n) that returns an integer comprised of the same digits contained in n but in reversed order. For example, invert(2015) should return the number 5102. Do not use any string operations to solve this problem.

    Test your program with the following main() function:

    def main():
        print(invert(2015))
        print(invert(123456789))
        print(invert(1000))
        print(invert(9999))
        print(invert(1234) + 1)
    

    The expected program output should be:

    5102
    987654321
    1
    9999
    4322

    Note that invert(1000) is 1, because any leading zeros in 0001 do not count and therefore are not printed.

  4. According to Wikipedia: “a perfect number is a positive integer that is equal to the sum of its proper positive divisors, that is, the sum of its positive divisors excluding the number itself”. For example, 28 is a perfect number because the proper divisors of 28 are: 1, 2, 4, 7, and 14; and 1 + 2 + 4 + 7 + 14 = 28.

    Write a program called perfect.py. Define in this program a function called perfect(n) that returns True if n is a perfect number, otherwise returns False.

    Test your program with the following main() function:

    def main():
        for i in range(1, 10000):
            if perfect(i):
                print(i)
    

    The expected program output should be:

    6
    28
    496
    8128
  5. Write a program called reduce.py. Define in this program a function called reduce(n) that reduces the value of n. The reduction consists in adding up all individual digits of the given number and repeating this process until you get a one digit number. For example, reduce(897) would first add 8 + 9 + 7 = 24; because 24 is not a one digit number the process has to be repeated: 2 + 4 = 6; given that 6 is a one digit number, that’s the result that should be returned by the reduce() function.

    Test your program with the following main() function:

    def main():
        print(reduce(897))
        print(reduce(67521))
        print(reduce(10000))
        print(reduce(9999999999999))
        print(reduce(123456789123456789123456789123456789))
    

    The expected program output should be:

    6
    3
    1
    9
    9
  6. Write a program called periods.py. Define in this program a funtion called periods(starting, target, interest) that returns the number of investment periods required for the starting balance to have grown equal or larger than the target balance given an interest rate (5% should be specified as 0.05). Although this can be computed directly mathematically, you should use a while loop to solve this problem.

    For example, periods(200, 250, 0.05) returns 5, because that’s the number of periods it will take to get to 250 (or more) starting with 200 with an 5% interest rate, as can be observed in the following table:

    Period # Balance
    0 200
    1 210
    2 220.5
    3 231.525
    4 243.10125
    5 255.2563125

    We stop at period 5 because 255.2563125 is greater or equal than the 250 target.

    Test your program with the following main() function:

    def main():
        print(periods(200, 250, 0.05))
        print(periods(15000, 14000, 0.07))
        print(periods(15000, 30000, 0.05))
        print(periods(15000, 30000, 0.07))
        print(periods(15000, 30000, 0.10))
        print(periods(15000, 30000, 1.00))
    

    The expected program output should be:

    5
    0
    15
    11
    8
    1