Coding in Python: A Comprehensive Beginners Guide to Learn the Realms of Coding in Python by Matthews Robert C
Author:Matthews, Robert C. [Matthews, Robert C.]
Language: eng
Format: epub, pdf
Published: 2020-10-27T00:00:00+00:00
While Loops
This section will shed light on how you can create and use Python while loops. You have already encountered the for loop which runs through a list of items and applies the code to each item in the list. The while loop is a bit different. It runs through a set of items as long as a certain condition stands true. While loop is interesting in the sense that you can use it to execute different interesting mathematical functions. The simplest and the most interesting thing is counting the numbers.
my_number = 1
while my_number <= 15:
print(my_number)
my_number += 1
= RESTART: C:/Users/saifia computers/Desktop/sample.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
>>>
See another mathematical example of the use of a while loop.
my_number = 1
while my_number <= 100:
print(my_number)
my_number += 5
= RESTART: C:/Users/saifia computers/Desktop/sample.py
1
6
11
16
21
26
31
36
41
46
51
56
61
66
71
76
81
86
91
96
>>>
I set the value to numbers 1 and 5, respectively. The while loop reads it and keeps running until it reaches 15 and 100, respectively. The code guides the loop to calculate the numbers and display the result on the interpreter. The loops get repeated as long as its condition remains true. Your player needs a while loop to exit the game. Only a while loop helps you end a game and shutdown it properly. Otherwise, it will hang the system each time you try to shut it down.
This demands that you let the users quit the game when they want to. I will not pack up the program in a while loop and then define the quit value for the same so that users can exit it by entering the quit value.
pgm = input("This program repeats whatever you tell it: ")
pgm += "
You have to enter 'q' to exit the program. "
msg = ""
while msg != 'q':
msg = input(pgm)
print(msg)
= RESTART: C:/Users/saifia computers/Desktop/sample.py
This program repeats whatever you tell it: I am determined to learn Python in six months.
I am determined to learn Python in six months.
You have to enter 'q' to exit the program. I am determined to build my programs in the first month of learning.
I am determined to build my programs in the first month of learning.
I am determined to learn Python in six months.
You have to enter 'q' to exit the program. q
q
>>>
I defined the prompt namely pgm in the first line of code. It gives the user two options; one to enter a message and another to quit the program. I also set a variable that stored the information the user enters. The while loop runs until the user enters q and breaks the loop. It can run a million times on end if the user does not end it.
pgm = input("This program repeats whatever you tell it: ")
pgm += "
You have to enter 'q' to exit the program. "
msg = ""
while msg != 'q':
msg = input(pgm)
print(msg)
= RESTART: C:/Users/saifia computers/Desktop/sample.py
This program repeats whatever you tell it: hi
hi
You have to enter 'q' to exit the program. how are you
how are you
hi
You have to enter 'q' to exit the program. What is your name?
What is your name?
hi
You have to enter 'q' to exit the program. Are you fine?
Are you fine?
hi
You have to enter 'q' to exit the program.
Download
Coding in Python: A Comprehensive Beginners Guide to Learn the Realms of Coding in Python by Matthews Robert C.pdf
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Ada | Ajax |
Assembly Language Programming | Borland Delphi |
C & C++ | C# |
CSS | Compiler Design |
Compilers | DHTML |
Debugging | Delphi |
Fortran | Java |
Lisp | Perl |
Prolog | Python |
RPG | Ruby |
Swift | Visual Basic |
XHTML | XML |
XSL |
Hello! Python by Anthony Briggs(9885)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(9770)
The Mikado Method by Ola Ellnestam Daniel Brolund(9763)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(8270)
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(7762)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(7750)
Grails in Action by Glen Smith Peter Ledbrook(7678)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7534)
Windows APT Warfare by Sheng-Hao Ma(6567)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6393)
Layered Design for Ruby on Rails Applications by Vladimir Dementyev(6314)
Blueprints Visual Scripting for Unreal Engine 5 - Third Edition by Marcos Romero & Brenden Sewell(6188)
Kotlin in Action by Dmitry Jemerov(5033)
Hands-On Full-Stack Web Development with GraphQL and React by Sebastian Grebe(4304)
Functional Programming in JavaScript by Mantyla Dan(4028)
Solidity Programming Essentials by Ritesh Modi(3878)
WordPress Plugin Development Cookbook by Yannick Lefebvre(3650)
Unity 3D Game Development by Anthony Davis & Travis Baptiste & Russell Craig & Ryan Stunkel(3600)
The Ultimate iOS Interview Playbook by Avi Tsadok(3571)
