Python Forum
Need help with understanding for/while loops!
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help with understanding for/while loops!
#1
Hi! I am new to python and up until now have found it rather simple. However for whatever reason, I cnnot figure out for.while loops. In particular latin squares. Which I do want to iterate, I just want help not the answer because I would like to actually learn this.
This is what I should get:
1 2 3 4 5
2 3 4 5 1
3 4 5 1 2
4 5 1 2 3
5 1 2 3 4

yet my code keeps giving me
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
OR
0 1 2 3 4 0
0 1 2 3 4 0
0 1 2 3 4 0
0 1 2 3 4 0
0 1 2 3 4 0
0 1 2 3 4 0

this is my coding so far:
"""""""""""""""""""""""""""""""""""""
square_order = int(input("Please input the order of the square: "))

left_number = int(input("Please input the top left number: "))

print("The latin square is: ")

latin_square = 0
square_height = 0
square_length = 0

while square_height <= square_order:
while square_length <= square_order:
print(square_length % square_order, end = " ")
square_length = square_length + 1
square_length = 0
square_height = square_height + 1
print( )
}square_order = int(input("Please input the order of the square: "))

left_number = int(input("Please input the top left number: "))

print("The latin square is: ")

latin_square = 0
square_height = 0
square_length = 0

while square_height <= square_order:
while square_length <= square_order:
print(square_length % square_order, end = " ")
square_length = square_length + 1
square_length = 0
square_height = square_height + 1
print( )
__square_order = int(input("Please input the order of the square: "))

left_number = int(input("Please input the top left number: "))

print("The latin square is: ")

latin_square = 0
square_height = 0
square_length = 0

while square_height <= square_order:
while square_length <= square_order:
print(square_length % square_order, end = " ")
square_length = square_length + 1
square_length = 0
square_height = square_height + 1
print( )
square_order = int(input("Please input the order of the square: "))

left_number = int(input("Please input the top left number: "))

print("The latin square is: ")

latin_square = 0
square_height = 0
square_length = 0

while square_height <= square_order:
while square_length <= square_order:
print(square_length % square_order, end = " ")
square_length = square_length + 1
square_length = 0
square_height = square_height + 1
print( )
"""""""""""""""""""""""""""""""""
Personally, I feel a for loop would be easier, but the assignment said "The aim of this project is practice the use of while loops and conditionals statements" Although, it does say using a modulus and range function will help.
Reply
#2
So speakith one ... from where?
Reply
#3
I'm very sorry but the code is very hard to read. Code Tags would help a lot.
The intuitive way to approach that task would be to use two nested while loops. One for the width and one for the height. it is even easier to use while loops and an index for iterating over a list containing all values that has to be placed than using a for loop.

I'm not sure how exactly you want the tips/help, here is my approach to the task:

1. ask the user input (like you did)
2. create a list containing all numbers from left_number to left_number + square_order
3. make one while loop for the number of lines that has to be printed
4. use a variable holding the current_first_number (like 0 in first line, 1 in second line, etc)
5. construct a list by combining a list generated through range(current_first_number, left_number + square_order + 1) and one generated through range(left_number, current_first_number)
6. print that list by joining the contained elements into a string and increase current_first_number
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Fundamental understanding-problem regards to while-loops Placebo 7 3,864 Oct-11-2018, 01:18 PM
Last Post: Placebo

Forum Jump:

User Panel Messages

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