Chapter 4 Exercise Set 1: PCEP Practice

I love Spam!

  1. Write a program that utilizes the concept of conditional execution, takes a string as input, and:

    • prints the sentence “Yes - Spam is the best food ever!” to the screen if the inputted string is “Spam” (upper-case)

    • prints “No, I want a big Spam!” if the inputted string is “spam” (lower-case)

    • prints “Spam! Not [input]!” otherwise. Note: [input] is the string taken as input.

The happy tax collector

  1. Once upon a time there was a land called Justice, inhabited by wise and happy people. The people in Justice where happy to pay taxes, since all wise and happy people understand the need to contribute generously to the people’s well being.

    The most important tax, called the Personal Ability Tax (PAT for short) had to be paid once a year, and was evaluated using the following rule:

    • if the citizen’s income was not higher than 22,111 tokens, the tax was equal to 18% of the income minus 156 tokens and 2 centavos de token (this was the so-called tax relief)

    • if the income was higher than this amount, the tax was equal to 8,102 tokens and 2 centavos de token, plus 62% of the surplus over 22,111 tokens.

    Your task is to write a tax calculator.

    • It should accept one floating-point value: the income.

    • Next, it should print the calculated tax, rounded to full tokens. There’s a function in Python named round() which will do the rounding for you.

    Note: this happy country never returns money to its citizens. If the calculated tax is less than zero, it only means no tax at all (the tax is equal to zero). Take this into consideration during your calculations.

    Test your code using the following data:

    Sample input: 100001 Expected output: The tax is:

    Sample input: 100000 Expected output: The tax is:

    Sample input: 1000 Expected output: The tax is:

    Sample input: -100 Expected output: The tax is: 0 tokens

“Being the computer”

Trace the execution of the following code segments and answer the questions that follow. When you can answer correctly without using the computer, you will have learned.

  1. What is the output of the following code snippet?

    a = 8
    b = 13
    c = 7
    
    print(a > b)
    print(b > c)
    
  2. What is the output of the following code snippet?

    a, b, c = 8, 13, 11
    
    print(a > c)
    print((b - 5) == a)
    
  3. What is the output of the following code snippet?

    a, b, c = 8, 13, 11
    a, b, c = c, b, a
    
    print(a > c)
    print((b - 5) == a)
    
  4. What is the output of the following code snippet?

    a = 42
    
    if a == 42:
        print(a == 42)
    if a > 7:
        print(a > 7)
    if a < 13:
        print(x < 13)
    else:
        print("else")
    
  5. What is the output of the following code snippet?

    a = "9"
    
    if a == 9:
        print("one")
    elif a == "9":
        if int(a) > 9:
            print("two")
        elif int(x) < 9:
            print("three")
        else:
            print("four")
    if int(a) == 9:
        print("five")
    else:
        print("six")
    
  6. What is the output of the following code snippet?

    s = 2
    t = 2.0
    u = "2"
    
    if s == t:
        print("one")
    if t == int(u):
        print("two")
    elif s == t:
        print("three")
    else:
        print("four")
    
  7. Look at the following code segment:

    n = 0
    while n < 22:
        if n % 2 == 0:
            n += 3
        else:
            n += 5
    print(n)
    

    How many times will the while loop execute? What value of n will be printed?

  8. What will be the output of the following program?

    a, b, c = 8, 23, 19
    if b < c:
        a, b, c = c, a, b
    else:
        a, b, c = b, c, a
    print(c + b, a)