Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
For \ While loops
#1
Hello , I'm a python beginner and I need help with For & While loops

1)Ive been asked to code a loop with 'While'. In the loop I need to have 3 variables which in two of them I need to enter different numbers , the loop needs to show me all the numbers between the 2 numbers I have entered including them. Ive managed to do it but only with 2 variables

num1= int(raw_input("Enter a number:"))
num2= int(raw_input("Enter a number:"))
while num1 <= num2:
(space) print num1
(space) num1 += 1

2) Same question as question 1 but now I need to use the Loop 'For' and use 'xrange'. Ive only managed to use 2 variables and I always get the last number -1

num1= int(raw_input("Enter a number:"))
num2= int(raw_input("Enter a number:"))
for num1 in xrange (num1,num2):
(space) print num1
Reply
#2
Hello and welcome to Python and the forums!
In Python, when you have begin and end index (such as in range/xrange, or in slicing iterables - iterable[2,4]), the item belonging to begin index is included in the result, while the item belonging to end index isn't. Therefore, if you want num2 to be included in the xrange, you'll need to provide it num2+1 as end index.
Next time when posting code, please use Python code tags.
Reply
#3
(Oct-31-2018, 02:30 PM)j.crater Wrote: Hello and welcome to Python and the forums!
In Python, when you have begin and end index (such as in range/xrange, or in slicing iterables - iterable[2,4]), the item belonging to begin index is included in the result, while the item belonging to end index isn't. Therefore, if you want num2 to be included in the xrange, you'll need to provide it num2+1 as end index.
Next time when posting code, please use Python code tags.
Thanks , sorry for posting here I am new to the forums
Reply
#4
You posted in the right place, what I suggested was wrapping your code in [python] code tags, which displays the code properly formatted and much more readable. No worries though.
Reply
#5
Thanks, I think I did what you said and its still not working

num1= int(raw_input("Enter a number:"))
num2= int(raw_input("Enter a number:"))
for num2 in xrange (num1,num2):
    print num2
    num2 += 1

Now it works
num1= int(raw_input("Enter a number:"))
num2= int(raw_input("Enter a number:"))
for num2 in xrange (num1,num2 + 1):
    print num2
Reply
#6
Yup, there you have it, glad it works :)
It would be better practice though to use different name for your new variable, instead of num2, which is already your input value and index in xrange.
Reply
#7
Ok thanks, soory for asking a lot but do you know how can I include another variable and still have the same result

num1= int(raw_input("Enter a number:"))
num2= int(raw_input("Enter a number:"))
while num1 <= num2:
    print num1 
    num1 += 1
Reply
#8
After the second line you could add num3 = num1, and then change all the num1's after that point to num3's. That may seem a bit silly, but note that at the end you will still have the original value of num1.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#9
This is what you mean?

num1= int(raw_input("Enter a number:"))
num2= int(raw_input("Enter a number:"))
num3= num1
while num3 <= num2:
    print num3
    num3 += 1
Reply
#10
Yes.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Forum Jump:

User Panel Messages

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