Posts: 8
Threads: 2
Joined: Oct 2018
Oct-31-2018, 02:23 PM
(This post was last modified: Oct-31-2018, 02:23 PM by Itay.)
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
Posts: 1,150
Threads: 42
Joined: Sep 2016
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.
Posts: 8
Threads: 2
Joined: Oct 2018
(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
Posts: 1,150
Threads: 42
Joined: Sep 2016
Oct-31-2018, 02:38 PM
(This post was last modified: Oct-31-2018, 02:39 PM by j.crater.)
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.
Posts: 8
Threads: 2
Joined: Oct 2018
Oct-31-2018, 02:49 PM
(This post was last modified: Oct-31-2018, 02:55 PM by Itay.)
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
Posts: 1,150
Threads: 42
Joined: Sep 2016
Oct-31-2018, 03:01 PM
(This post was last modified: Oct-31-2018, 03:01 PM by j.crater.)
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.
Posts: 8
Threads: 2
Joined: Oct 2018
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
Posts: 4,220
Threads: 97
Joined: Sep 2016
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.
Posts: 8
Threads: 2
Joined: Oct 2018
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
Posts: 4,220
Threads: 97
Joined: Sep 2016
|