fb pixel

Introduction to Python

  • Profound Academy

    • Status
      • 1
        Input and Output
      • 2
        Variables and Integer Arithmetic
      • 3
        Conditional Operators
      • 4
        Advanced Conditions - Nesting
      • 5
        Types and Variables
      • 6
        Strings
      • 7
        Lists
      • 8
        For Loops
      • 9
        While Loops
      • 10
        continue, break, while...else
      • 11
        String and List methods
      • 12
        Nested Loops
      • 13
        List Comprehension
      • 14
        Tuples and Sets
      • 15
        dict
      • 16
        Functions
      • 17
        Functions 2
      • 18
        Lambda and higher-order functions
      • 19
        Files

  • Reading input

    while loops can have conditions that are always satisfied. Those programs will result in an infinite loop and will run forever. A good example of that type of program is while True:
    while True:
    	print('Welcome to an infinite loop!')
    This program will run forever and will print Welcome to an infinite loop! until it’s stopped by the user.
    while loops can be used to read data until some condition has been met. For instance, we can read the data from the input until the input is not 0. As soon as the user inputs a 0 and hits enter, the while loop should end. This can be implemented as:
    num = -1
    while num != 0:
    	num = int(input())
    	print(f'The user entered: {num}')
    print('End!')
    This program will read the input as long as the user hasn’t entered 0, and as soon as the user enters 0, it will get out of the loop and print End!.
     
    We can even read the num beforehand and then start the loop:
    num = int(input())
    while num != 0:
    	print(f'The user entered: {num}')
    	num = int(input())
    print('End!')
    This way, the program won’t print the final 0 as it will get out of the loop before that.
    You can try it yourself to play around with those two ways.

    Challenge

    Write a program that reads the input until reaching a word End and prints the inputted text to the output. As soon as the program reaches the word End it should stop. The program should not print the final End from the input.
    Input
    Output
    hello my name End
    hello my name
     
    To check your solution you need to sign in
    Sign in to continue