Run Lines of Code Multiple Times

AI Thread Summary
To allow users to specify how many times a block of code runs in Python 3, a "for" loop can be utilized. The user inputs the desired number of iterations, which is stored in the variable 'times'. A range object is created using this value, and a for loop iterates through this range. Within the loop, the lines of code intended for execution are placed. After the loop completes, a confirmation message indicates that the code has run the requested number of times. An example implementation includes defining 'times' with user input, creating a range from 0 to 'times', and executing the desired lines of code within the loop.
Cash Fulton
Messages
24
Reaction score
1
In Python 3...

I have a few lines of code that I want the user to choose how many times it will run. For example:

times=int(input())

'times' will be the number of times the lines of code will run.

How do I do it?
 
Technology news on Phys.org
Cash Fulton said:
Thanks for the response. Though, I don't fully understand how I would write it for my exact purpose.

Could you give me an example with the 'for loop' for what I'm trying to accomplish?
Python:
times=int(input())
myrange=range(0,times)
for count in myrange:
    yourFirstLineOfCode
    yourSecondLineOfCode
    yourThirdLineOfCode
    youGetTheIdea
print("Code has been executed the requested number of times")
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top