How the program has control with the break statement [closed]
2024/11/19 22:39:56
Could anybody explain this program and output of this? I am having doubt at if statement. I'm not able to understand how break statement works in this:
for n in range(2, 10):for x in range(2, n):if n % x == 0:print n, 'equals', x, '*', n/xbreakelse:# loop fell through without finding a factorprint n, 'is a prime number'
Output:
2 is a prime number
3 is a prime number
4 equals 2 * 2
5 is a prime number
6 equals 2 * 3
7 is a prime number
8 equals 2 * 4
9 equals 3 * 3
Answer
I'll add some comments:
for n in range(2, 10): #Loops from 2 to 9, inclusive. Call this Loop A.for x in range(2, n): #Loops from 2 to n-1, inclusive. Call this Loop B.if n % x == 0: #If n is divisible by x, execute the indented codeprint n, 'equals', x, '*', n/x #print the discovered factorizationbreak #Break out of loop B, skipping the "else" statementelse: #If the loop terminates naturally (without a break) this will be executed# loop fell through without finding a factorprint n, 'is a prime number'
I need to create an interactive chart on python taking data from different sheets of an Excel file. I tried to create a for loop to take data from all the sheets automatically, but I manage to graph on…
Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 6…
I am stuck with this question:
Write a function driving_cost() with input parameters miles_per_gallon, dollars_per_gallon, and miles_driven, that returns the dollar cost to drive those miles. All items…
I have a Pandas DataFrame and I want to find all rows where the ith column values are 10 times greater than other columns.
Here is an example of my DataFrame:For example, looking at column i=0, row B (…
Im using python 2.7 on Linux CentOS 6.5. After successfully using yum to install numpy, I am unable to import the module.from numpy import *The above code produces the following error:no module named …
I am getting "list indices must be integers or slices, not tuple" error while trying to generate list from list of tuples.
list of tuples have the following structure:[(29208, 8, 8, 8), (2920…
I am trying to use the google Double click bid manager (DBM) API, to download reports, I am trying to make this automatic without manual authentication, but all I can find is the GitHub repo for DBM sa…
How can I update a value in an existing .csv file using a python program. At the moment the file is read into the program but I need to be able to change this value using my program, and for the change…
The GoalI have a directory with 65 .txt files, which I am parsing, one by one, and saving the outputs into 65 corresponding .txt files. I then plan to concatenate them, but Im not sure if jumping strai…