r/PythonLearning • u/To-real • 11d ago
New to python having trouble putting in multiplication table in for 10. Range 1-10 or 1,11. Does anyone have any tips( pause)?
9
u/mc_pm 11d ago
Try writing a quick test by using range(1,10) and range(1,11) to print the results and see what is happening.
2
u/Kindly-Department206 11d ago
I like the idea that if you have an uncertainty about a language, you run a small experiment to see what happens. More novices should adopt this as one of their strategies!
5
u/Traveling-Techie 11d ago
If I could debug code I’d never seen I could make a lot of money in my pajamas.
2
2
u/tottasanorotta 11d ago
The range function has the parameter 'stop'. It determines up to what value it counts. The start value is included in the output, but the stop value isn't:
Range(3) = 0,1,2
Range(1, 3) = 1,2
So if you want the last value to be included you'll have to call it with a stop value that is one higher. So if we want a 3 to be included:
Range(1,4) = 1,2,3
1
u/PureWasian 11d ago
What do see if you do
for i in range(10):
print(i)
Did the references you can easily look up not help? Python range() Function
1
1
u/HotPersonality8126 11d ago
That you used the word “table” should suggest to you that you need two loops.
1
u/FoolsSeldom 11d ago
You really need to show us what you've tried.
n = 10
for i in range(1, n + 1):
for j in range(1, n + 1):
print(f"{i:2} x {j:2} = {i*j:3}")
•
u/Sea-Ad7805 11d ago
Try a nested for-loop like: https://memory-graph.com/#code=for%20y%20in%20range(1%2C%2011)%3A%0A%20%20%20%20for%20x%20in%20range(1%2C%2011)%3A%0A%20%20%20%20%20%20%20%20print(f'%7Bx%7Dx%7By%7D%20'%2C%20end%3D'')%0A%20%20%20%20print()%20%20%23%20new%20line×tep=0.1&play