Python Forum
Nested for loops with numbers
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Nested for loops with numbers
#1
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
Reply
#2
Your not printing the value of the loop iterator (which is i)
Reply
#3
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)
Reply
#4
# 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)
Reply
#5
(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
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#6
(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.
Reply
#7
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?
Reply
#8
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
>>>
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Random Generator: From Word to Numbers, from Numbers to n possibles Words Yamiyozx 2 1,369 Jan-02-2023, 05:08 PM
Last Post: deanhystad
  Problem with nested loops robert5502001 7 3,502 Aug-01-2022, 06:26 AM
Last Post: stevensanders
  Convert list of numbers to string of numbers kam_uk 5 2,935 Nov-21-2020, 03:10 PM
Last Post: deanhystad
  computing average in nested loops cap510 5 5,108 Sep-11-2020, 12:33 PM
Last Post: deanhystad
  Capitalize first letter of names in nested loops student_of_python 9 4,633 Oct-27-2019, 07:51 AM
Last Post: Larz60+
  nested for loops to recursion ashkea26 4 3,438 Nov-02-2018, 05:00 PM
Last Post: ichabod801
  Nested loops in openCV JimmyJangle 1 4,786 Apr-17-2018, 04:10 AM
Last Post: Mekire
  Regular Expressions in Files (find all phone numbers and credit card numbers) Amirsalar 2 4,053 Dec-05-2017, 09:48 AM
Last Post: DeaD_EyE
  Nested loops, lists and if statements Liquid_Ocelot 10 8,863 Apr-23-2017, 02:02 PM
Last Post: Mekire

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020