ELLAK - Python course - ws02

EL/LAK - Python Course

worksheet 02

  1. What is missing from this code?
    1
    2
    3
    4
    5
    temperature = float(input("Temperature: ")
    if temperature > 30:
    print("It is hot outside.")
    else:
    print("It is not hot out.")
  2. Write a Python program that will take in a number from the user and print if it is positive, negative, or zero. Use a proper if/elif/else chain, don't just use three if statements.
  3. Write a Python program that will take in a number from a user and print out ``Success'' if it is greater than -10 and less than 10, inclusive.
  4. This runs, but there is something wrong. What is it?
    1
    2
    3
    4
    5
    6
    7
    user_input = input("A cherry is a: ")
    print("A. Dessert topping")
    print("B. Desert topping")
    if user_input == "A":
    print("Correct!")
    else:
    print("Incorrect.")
  5. There are two things wrong with this code that tests if x is set to a positive value. One prevents it from running, and the other is not obvious. Make sure the if statement works no matter what x is set to. Identify both issues.
    1
    2
    3
    4
    5
    x == input(" x = "))
    if x >= 0:
    print("x is positive.")
    else:
    print("x is not positive.")
  6. What three things are wrong with the following code?
    1
    2
    3
    x = input("Enter a number: ")
    if x = 3
    print("You entered 3")
  7. This program doesn't work correctly. What is wrong?
    1
    2
    3
    x = input("How are you today?")
    if x == "Happy" or "Glad":
    print("That is good to hear!")
  8. Look at the code below. Write your best guess here on what it will print. Next, run the code and see if you are correct. Clearly label your guess and the actual answer. Also, if this or any other example results in an error, make sure to state that an error occurred. While you don't need to write an explanation, make sure you understand why the computer prints what it does. Don't get caught flat-footed when you need to know later.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    x = 5
    y = x == 6
    z = x == 5
    print("x=", x)
    print("y=", y)
    print("z=", z)
    if y:
    print("Fizz")
    if z:
    print("Buzz")
  9. Look at the code below. Write you best guess on what it will print. Next, run the code and see if you are correct.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    x = 5
    y = 10
    z = 10
    print(x < y)
    print(y < z)
    print(x == 5)
    print(not x == 5)
    print(x != 5)
    print(not x != 5)
    print(x == "5")
    print(5 == x + 0.00000000001)
    print(x == 5 and y == 10)
    print(x == 5 and y == 5)
    print(x == 5 or y == 5)
  10. Look at the code below. Write you best guess on what it will print. Next, run the code and see if you are correct.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    print("3" == "3")
    print(" 3" == "3")
    print(3 < 4)
    print(3 < 10)
    print("3" < "4")
    print("3" < "10")
    print( (2 == 2) == "True" )
    print( (2 == 2) == True )
    print(3 < "3")
  11. What things are wrong with this section of code? The programmer wants to set the money variable according to the initial occupation the user selects.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    print("Welcome to Bank of Monastiraki!")
    print("A. Banker")
    print("B. Carpenter")
    print("C. Farmer")
    user_input = input("What is your occupation? ")
    if user_input = A:
    money = 100
    else if user_input = B:
    money = 70
    else if user_input = C:
    money = 50
    print("Great! you will start the game with", money, "dollars.")