Run Lines of Code Multiple Times

Click For Summary

Discussion Overview

The discussion revolves around how to run a specific set of lines of code multiple times in Python 3, based on user input for the number of iterations. The focus is primarily on programming techniques and examples related to loops.

Discussion Character

  • Technical explanation
  • Homework-related

Main Points Raised

  • One participant asks how to execute a few lines of code a user-defined number of times using Python 3.
  • Another participant suggests using a "for" loop and provides a link to an external resource on for loops.
  • A later reply expresses a lack of understanding regarding the application of the for loop to the specific problem and requests a concrete example.
  • Another participant provides an example of using a for loop, including a range based on user input, and outlines how to structure the code to achieve the desired functionality.

Areas of Agreement / Disagreement

Participants appear to be engaged in a collaborative effort to clarify the use of loops in Python, but there is no consensus on the best way to implement the solution, as some participants express confusion about the example provided.

Contextual Notes

The discussion includes varying levels of understanding regarding the implementation of loops in Python, with some participants seeking clarification on specific coding techniques.

Who May Find This Useful

Individuals learning Python programming, particularly those interested in control structures like loops and user input handling.

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")
 

Similar threads

Replies
3
Views
3K
  • · Replies 11 ·
Replies
11
Views
2K
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
4K
Replies
55
Views
7K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 0 ·
Replies
0
Views
3K
Replies
7
Views
3K