Python Basics: While Loops and Variables
20 Questions
0 Views

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

Which variable name correction was crucial to fix the syntax errors in the given code?

  • Changing 'value' to 'val' in the elif statement
  • Changing 'VAL' to 'value' in the for loop
  • Changing 'value' to 'VAL' in the print statement
  • Changing 'val' to 'VAL' in the if statement (correct)
  • What error was fixed in the following corrected code?

    for VAL in range (0,value):
        if VAL %4 == 0:
            print (VAL *4)
        elif VAL %5 == 0:
            print (VAL+3)
        else:
            print (VAL+10)
    

  • Indentation error
  • Logical error
  • Syntax error (correct)
  • Type error
  • Which of the following statements is true about the corrected code?

  • It prints multiples of 4 and 5 for values divisible by both.
  • It prints multiples of 5 for values divisible by 5.
  • It prints a fixed number for values divisible by both 4 and 5.
  • It prints multiples of 4 for values divisible by 4. (correct)
  • In the fixed code, what happens when the value of VAL is 10?

    <p>It prints 13</p> Signup and view all the answers

    Which logical construct is primarily used in the example code provided?

    <p>conditional statements</p> Signup and view all the answers

    What is the main purpose of the provided program?

    <p>To check if a string is a palindrome.</p> Signup and view all the answers

    Which of the following is a logical error in the provided program?

    <p>The output message for a palindrome is outside the loop.</p> Signup and view all the answers

    What will be the output for the input string radar?

    <p>It is palindrome.</p> Signup and view all the answers

    What is the purpose of the line mid = length // 2?

    <p>To determine the stopping point for the loop.</p> Signup and view all the answers

    Which variable name should be corrected for proper functioning of the program?

    <p>str to string</p> Signup and view all the answers

    What will be the output of the following code?

    x = 'abcdef'
    i = 'a'
    while i in x:
      print(i, end='')
    

    <p>aaaa</p> Signup and view all the answers

    What will be the output of the following code?

    values = []
    for i in range(1, 4):
      values.append(i)
    print(values)
    

    <p>[1, 2, 3]</p> Signup and view all the answers

    Analyze the following code and identify the final values of 'x' and 'y'.

    x = 10
    y = 0
    while x &gt; y:
      x = x - 1
      y = y + 1
    

    What will be the final value of 'y'?

    <p>5</p> Signup and view all the answers

    Consider the following code snippet and its output:

    x = 10
    y = 0
    while x &gt; y:
      print(x, y)
      x = x - 1
      y = y + 1
    

    Which pair of numbers is printed at the last iteration?

    <p>5 5</p> Signup and view all the answers

    How many iterations will the following loop run?

    x = 10
    y = 0
    while x &gt; y:
      x = x - 1
      y = y + 1
    

    <p>10</p> Signup and view all the answers

    What is the purpose of looping from 2 to n/2 in the prime number function?

    <p>To find if n is divisible by any number in the given range</p> Signup and view all the answers

    What will be the output if we run the prime number function with n = 15?

    <p>Number is not prime</p> Signup and view all the answers

    Which of the following correctly describes the output of the cube function for i = 20?

    <p>Cube of number 20 is 8000</p> Signup and view all the answers

    How many times will "Hello" be printed in the provided while loop?

    <p>10 times</p> Signup and view all the answers

    What will be the printed output of the code for i in range(15, 18)?

    <p>Cube of number 15 is 3375 Cube of number 16 is 4096 Cube of number 17 is 4913</p> Signup and view all the answers

    Study Notes

    Python Code Examples

    • Example 1: Printing characters in a string

    • x = "abcdef" and i = "a" are assigned

    • A while loop prints "a" repeatedly until i is no longer in x

    • Output: aaaa

    • Example 2: Appending values to a list

    • An empty list values is created

    • A for loop appends numbers from 1 to 3 to values using range(1, 4)

    • Output: [1, 2, 3]

    Python Revision Tour

    • Code example: Printing x and y values in a while loop
    • x is initially 10, and y is initially 0
    • The loop continues until x is no longer greater than y
    • Output: x and y values decrementing and incrementing, respectively

    Question 1: Correcting syntax errors

    • Original code:
      • VAL and val mismatch in variable naming
      • Corrected code:
        • val is replaced with VAL throughout the code
        • Underlined corrections: VAL%4, VAL%5, and VAL+10

    Question 2: Palindrome checker

    • Original code: incomplete and cropped
    • Corrected code:
      • str is assigned user input using input("enter any string")
      • length is calculated as the length of the input string
      • mid is calculated as half the length of the string
      • A loop checks if the string is a palindrome by comparing characters from the start and end
      • Output: "It is a palindrome" if the string meets the condition, otherwise "it is not a palindrome"

    Prime Number Checker

    • Function prime(n):
      • Takes an integer n as input
      • Loops through numbers from 2 to n/2
      • Checks if n is divisible by any number in the range
      • Prints "Number is not prime" if divisible, otherwise "Number is prime"

    Printing Cubes of Numbers

    • Code example:
      • Loops from 15 to 50 using range(15, 50)
      • Prints the cube of each number in the range

    Printing "Hello" in a Loop

    • Code example:
      • Initializes count to 0
      • A while loop continues until count reaches 10
      • Prints "Hello" and increments count in each iteration
      • Output: "Hello" printed 10 times

    Studying That Suits You

    Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

    Quiz Team

    Description

    Practice writing Python code using while loops and variables to understand the output of given programs.

    Use Quizgecko on...
    Browser
    Browser