Python Forum
Nested for loops with numbers - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Nested for loops with numbers (/thread-2923.html)



Nested for loops with numbers - Liquid_Ocelot - Apr-19-2017

Hey guys so I am running into a problem with my code:

Basically I need to write a code that prompts for two numbers (Limit and Copies) and then writes all the numbers from 1 (startvalue) up to and including Limit, Copies number of times and I also need to use nested loops.

Then I have to write a function that that prints out a row of numbers separated by tabs, from (startvalue) to and including limit.



So far I have something looking like this..

startvalue = (1)
limit = int(input("Please enter a limit"))
copies = int(input("How many copies would you like?"))
list = [startvalue,limit]
for i in range (copies):
    print (startvalue,limit)
Please enter a limit3
How many copies would you like?2
1 3
1 3
So I guess I just want to know how to print the numbers say from 1 to 5 in a row depending on how many copies I input e.g 3 copies

e.g 1 2 3 4 5


RE: Nested for loops with numbers - Larz60+ - Apr-19-2017

Your not printing the value of the loop iterator (which is i)


RE: Nested for loops with numbers - smbx33 - Apr-20-2017

Your code literally prints out the initial values entered, and that loops for the number of copies you want. To get the result you wish.... you have to make python count up from a number to the end number. You don't have anything in your code that does that. Below is code that solves the requirement of using nested loop and it counts from start_value to end_value, printing out a list with the results.




limit = int(input("please enter limit"))
copies = int(input("please enter copies"))
start_value = 1
end_value = 0
results = []
for copy in range(copies):
   while end_value < limit:
       end_value += 1
       results.append(end_value)
   print(results)



RE: Nested for loops with numbers - idontreallywolf - Apr-20-2017

# initalizing variables
startvalue = 1
lst = []

# handling ValueErrors (in case user inputs a letter instead of number)
try:
   limit = int(raw_input("Enter Limit: "))
   copies = int(raw_input("How many copies would you like? : "))
except ValueError:
   print "Error: Invalid input."

# iterating x times startvalue to limit.
for i in range(startvalue,limit):
   lst.append(i)

# since start value is 1 there will be a missing number and this is a work-around solution.
lst.append(lst[-1]+1) 

# printing the numbers ( copies ) times. With tabs.
for i in range(copies):
   print '     '.join(str(x) for x in lst)



RE: Nested for loops with numbers - volcano63 - Apr-20-2017

(Apr-20-2017, 08:56 PM)smbx33 Wrote:
.....
for copy in range(copies):
     ......

Since there's a standard module named copy, the variable with the same name may overshadow it(slim chance, but still). Names that may overshadow standard functions and modules are not recommended. 

How would you know which are? Single word that may sound like a standard function, e,g, map, filter, string. Rule of the thumb - when in doubt, add underscore at the end. Or use the beloved  Wink Pythonic snake style


RE: Nested for loops with numbers - smbx33 - Apr-21-2017

(Apr-20-2017, 09:58 PM)volcano63 Wrote:
(Apr-20-2017, 08:56 PM)smbx33 Wrote:
.....
for copy in range(copies):
     ......

Since there's a standard module named copy, the variable with the same name may overshadow it(slim chance, but still). Names that may overshadow standard functions and modules are not recommended. 

How would you know which are? Single word that may sound like a standard function, e,g, map, filter, string. Rule of the thumb - when in doubt, add underscore at the end. Or use the beloved  Wink Pythonic snake style
for copy_ in range(copies):
would be correct then? thanks.


RE: Nested for loops with numbers - Liquid_Ocelot - Apr-21-2017

Cheers everyone I have some learning to do with nested loops. Appreciate the help

Just a quick question how would I prints out a row of numbers separated by tabs, from startValue to and including limit?


RE: Nested for loops with numbers - nilamo - Aug-15-2017

4 months later, I suppose just giving the answer doesn't hurt.

>>> tabbed_range = lambda startValue, limit: "\t".join(map(str, range(startValue, limit+1)))
>>> print(tabbed_range(5, 8))
5       6       7       8
>>>