Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Explain range in this code
#1
Hi,
I created this code to calculate a product used two input of user :

factor = int(input())
multiple = int(input())

for p in range ( 1, multiple + 1):
  print ('{} x {}={}'.format(factor, p, factor *p))
The for lo cycle seen in a solution and the thing that does not enter my head is the reason why I used it in a in range (multiple + 1), I thought it was to iterate until I reached the value that was passed in input.
I was wondering if my reasoning is correct.
Regards,
RavCoder
Reply
#2
If you have range(x, y), it starts with x, and keeps going as long as the result is less than y. When the result is equal to (or greater than) y, it stops without returning a value. So if you want the last number to be multiple, you have to do range(x, multiple + 1).
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
did you check the docs? It's your best friend and first place to turn to if something is unclear....
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
I checked the documentation, but I didn't understand this passage of the reason why to put multiple + 1 instead I don't know type multiple and enough or to add a number to stop the cycle.
Reply
#5
Translation:
Python:
for count in range(a,b)
Other languages, variation of:
Quote:for (count=a;count<b,count++)

Point being, it is not count <= b, it's count < b, which is why you add 1.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [split] Explain the python code in this definition Led_Zeppelin 1 736 Jan-13-2023, 10:20 PM
Last Post: deanhystad
  I am new to python and Could someone please explain how this below code is working? kartheekdas 2 1,003 Dec-19-2022, 05:24 PM
Last Post: kartheekdas
  Explain the python code in this definition Led_Zeppelin 1 1,086 Oct-27-2022, 04:04 AM
Last Post: deanhystad
  Sudoku Solver in Python - Can someone explain this code ? qwemx 6 2,120 Jun-27-2022, 12:46 PM
Last Post: deanhystad
  Can someone explain this small snippet of code like I am a 5 year old? PythonNPC 3 1,235 Apr-08-2022, 05:54 PM
Last Post: deanhystad
  Could you explain each part of the code? Tsushida 2 1,500 Mar-20-2022, 08:19 AM
Last Post: Larz60+
  matplotlib x axis range goes over the set range Pedroski55 5 3,173 Nov-21-2021, 08:40 AM
Last Post: paul18fr
  What is the run time complexity of this code and please explain? samlee916 2 2,289 Nov-06-2020, 02:37 PM
Last Post: deanhystad
  code with no tuple gets : IndexError: tuple index out of range Aggam 4 2,801 Nov-04-2020, 11:26 AM
Last Post: Aggam
  poplib - parsing message body, could somebody please help explain this code t4keheart 2 2,289 Oct-12-2020, 01:59 PM
Last Post: t4keheart

Forum Jump:

User Panel Messages

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