Finding the Range
up vote
0
down vote
favorite
Hi so for homework I have to type up a program where it will accept two numbers. If one of the numbers on the left is smaller than the one on the right, it will increase. If the second number is smaller than the first number than it will decrease. If both numbers are the same then it should stay the same. Here the program I had written so far:
def range_of_numbers (number1, number2):
if (number2 > number1):
for num1 in range (1):
print (2, 3, 4, 5, 6, 7 )
elif (number1 > number2):
for num2 in range (1):
print (19, 18, 17, 16, 15, 14, 13, 12, 11)
else:
print (42)
The sample calls are:
range_of _numbers (2, 8)
range_of_numbers (18, 11)
range_of_numbers (42, 42)
I got two of them right, but the last one keeps getting the wrong output and i dont know what wrong or where the mistake to fix it.
python for-loop if-statement while-loop range
|
show 2 more comments
up vote
0
down vote
favorite
Hi so for homework I have to type up a program where it will accept two numbers. If one of the numbers on the left is smaller than the one on the right, it will increase. If the second number is smaller than the first number than it will decrease. If both numbers are the same then it should stay the same. Here the program I had written so far:
def range_of_numbers (number1, number2):
if (number2 > number1):
for num1 in range (1):
print (2, 3, 4, 5, 6, 7 )
elif (number1 > number2):
for num2 in range (1):
print (19, 18, 17, 16, 15, 14, 13, 12, 11)
else:
print (42)
The sample calls are:
range_of _numbers (2, 8)
range_of_numbers (18, 11)
range_of_numbers (42, 42)
I got two of them right, but the last one keeps getting the wrong output and i dont know what wrong or where the mistake to fix it.
python for-loop if-statement while-loop range
So you have to return hard-coded values?
– Ayxan
Nov 10 at 20:16
'If both numbers are the same then it should stay the same' I'm not sure what you mean by this?
– smoggers
Nov 10 at 20:25
Yeah the values that were given, i have to return them. So i have to type a print statement that shows the numbers going up, one going down, and the last one that just the same
– Christian LLanos
Nov 10 at 20:40
basically for the sample it says 45, 45. Since these two are the same, the print statement has to be the same. However, if it came out like 2, 8, the number on the left is smaller than the one on the right, so it gonna increase going up. If it was the opposite (8, 2), the number is gonna decrease and go down.
– Christian LLanos
Nov 10 at 20:45
@ChristianLLanos What are your expected outputs for (2, 8), (18, 11) and (42, 42)? Which part of my answer's output is wrong?
– Ayxan
Nov 10 at 21:12
|
show 2 more comments
up vote
0
down vote
favorite
up vote
0
down vote
favorite
Hi so for homework I have to type up a program where it will accept two numbers. If one of the numbers on the left is smaller than the one on the right, it will increase. If the second number is smaller than the first number than it will decrease. If both numbers are the same then it should stay the same. Here the program I had written so far:
def range_of_numbers (number1, number2):
if (number2 > number1):
for num1 in range (1):
print (2, 3, 4, 5, 6, 7 )
elif (number1 > number2):
for num2 in range (1):
print (19, 18, 17, 16, 15, 14, 13, 12, 11)
else:
print (42)
The sample calls are:
range_of _numbers (2, 8)
range_of_numbers (18, 11)
range_of_numbers (42, 42)
I got two of them right, but the last one keeps getting the wrong output and i dont know what wrong or where the mistake to fix it.
python for-loop if-statement while-loop range
Hi so for homework I have to type up a program where it will accept two numbers. If one of the numbers on the left is smaller than the one on the right, it will increase. If the second number is smaller than the first number than it will decrease. If both numbers are the same then it should stay the same. Here the program I had written so far:
def range_of_numbers (number1, number2):
if (number2 > number1):
for num1 in range (1):
print (2, 3, 4, 5, 6, 7 )
elif (number1 > number2):
for num2 in range (1):
print (19, 18, 17, 16, 15, 14, 13, 12, 11)
else:
print (42)
The sample calls are:
range_of _numbers (2, 8)
range_of_numbers (18, 11)
range_of_numbers (42, 42)
I got two of them right, but the last one keeps getting the wrong output and i dont know what wrong or where the mistake to fix it.
python for-loop if-statement while-loop range
python for-loop if-statement while-loop range
edited Nov 11 at 1:15
Ayxan
1,06715
1,06715
asked Nov 10 at 20:10
Christian LLanos
102
102
So you have to return hard-coded values?
– Ayxan
Nov 10 at 20:16
'If both numbers are the same then it should stay the same' I'm not sure what you mean by this?
– smoggers
Nov 10 at 20:25
Yeah the values that were given, i have to return them. So i have to type a print statement that shows the numbers going up, one going down, and the last one that just the same
– Christian LLanos
Nov 10 at 20:40
basically for the sample it says 45, 45. Since these two are the same, the print statement has to be the same. However, if it came out like 2, 8, the number on the left is smaller than the one on the right, so it gonna increase going up. If it was the opposite (8, 2), the number is gonna decrease and go down.
– Christian LLanos
Nov 10 at 20:45
@ChristianLLanos What are your expected outputs for (2, 8), (18, 11) and (42, 42)? Which part of my answer's output is wrong?
– Ayxan
Nov 10 at 21:12
|
show 2 more comments
So you have to return hard-coded values?
– Ayxan
Nov 10 at 20:16
'If both numbers are the same then it should stay the same' I'm not sure what you mean by this?
– smoggers
Nov 10 at 20:25
Yeah the values that were given, i have to return them. So i have to type a print statement that shows the numbers going up, one going down, and the last one that just the same
– Christian LLanos
Nov 10 at 20:40
basically for the sample it says 45, 45. Since these two are the same, the print statement has to be the same. However, if it came out like 2, 8, the number on the left is smaller than the one on the right, so it gonna increase going up. If it was the opposite (8, 2), the number is gonna decrease and go down.
– Christian LLanos
Nov 10 at 20:45
@ChristianLLanos What are your expected outputs for (2, 8), (18, 11) and (42, 42)? Which part of my answer's output is wrong?
– Ayxan
Nov 10 at 21:12
So you have to return hard-coded values?
– Ayxan
Nov 10 at 20:16
So you have to return hard-coded values?
– Ayxan
Nov 10 at 20:16
'If both numbers are the same then it should stay the same' I'm not sure what you mean by this?
– smoggers
Nov 10 at 20:25
'If both numbers are the same then it should stay the same' I'm not sure what you mean by this?
– smoggers
Nov 10 at 20:25
Yeah the values that were given, i have to return them. So i have to type a print statement that shows the numbers going up, one going down, and the last one that just the same
– Christian LLanos
Nov 10 at 20:40
Yeah the values that were given, i have to return them. So i have to type a print statement that shows the numbers going up, one going down, and the last one that just the same
– Christian LLanos
Nov 10 at 20:40
basically for the sample it says 45, 45. Since these two are the same, the print statement has to be the same. However, if it came out like 2, 8, the number on the left is smaller than the one on the right, so it gonna increase going up. If it was the opposite (8, 2), the number is gonna decrease and go down.
– Christian LLanos
Nov 10 at 20:45
basically for the sample it says 45, 45. Since these two are the same, the print statement has to be the same. However, if it came out like 2, 8, the number on the left is smaller than the one on the right, so it gonna increase going up. If it was the opposite (8, 2), the number is gonna decrease and go down.
– Christian LLanos
Nov 10 at 20:45
@ChristianLLanos What are your expected outputs for (2, 8), (18, 11) and (42, 42)? Which part of my answer's output is wrong?
– Ayxan
Nov 10 at 21:12
@ChristianLLanos What are your expected outputs for (2, 8), (18, 11) and (42, 42)? Which part of my answer's output is wrong?
– Ayxan
Nov 10 at 21:12
|
show 2 more comments
2 Answers
2
active
oldest
votes
up vote
2
down vote
Firstly, what you're doing with your first two cases is kind of "cheating" and is what's known as "hardcoding" results. You're not writing a function to perform the task but instead you're just printing the answers you know you should be seeing.
Ironically though, you're pretty close here.
This should do what you want:
def range_of_numbers (num1, num2):
if(num1 < num2):
for i in range(num2-num1 +1):
print(num1+i)
elif(num1 > num2):
for i in range(num1-num2 +1):
print(num1-i)
else:
print(num1)
if either num1 or num 2 is greater, we find the difference and make a for loop of that size (+1 since we're 0 indexed and we want to include both ends here). Then we either count up or count down, depending on which is higher.
tried it out but it didnt work sadly
– Christian LLanos
Nov 10 at 20:57
Can you say what it is that isn't working please? What result are you expecting that you aren't getting? I do believe the code above does what you asked for in your original question. Please confirm you're asking your question properly
– Capn Jack
Nov 10 at 20:59
so the range isn't working as it should, the response I'm getting back for the first on (2, 8) is giving me 0 at the start when it shouldnt. It similar to the second one also (18, 11). It starting at 11 when it shouldnt be.
– Christian LLanos
Nov 10 at 21:10
Did you copy the code exactly?? I'm running the exact same code and I'm getting the results you asked for.
– Capn Jack
Nov 10 at 21:12
yup copied the whole thing
– Christian LLanos
Nov 10 at 21:15
|
show 1 more comment
up vote
0
down vote
Use 1 or -1 as steps in your range:
def range_of_numbers(a, b):
if (a == b):
print("same")
else:
print(*list(range(a, b, (1 if a<b else -1))))
Test it:
range_of_numbers(2, 8)
range_of_numbers(18, 11)
range_of_numbers(42, 42)
Sure enough you get the desired output:
2 3 4 5 6 7
18 17 16 15 14 13 12
same
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
Firstly, what you're doing with your first two cases is kind of "cheating" and is what's known as "hardcoding" results. You're not writing a function to perform the task but instead you're just printing the answers you know you should be seeing.
Ironically though, you're pretty close here.
This should do what you want:
def range_of_numbers (num1, num2):
if(num1 < num2):
for i in range(num2-num1 +1):
print(num1+i)
elif(num1 > num2):
for i in range(num1-num2 +1):
print(num1-i)
else:
print(num1)
if either num1 or num 2 is greater, we find the difference and make a for loop of that size (+1 since we're 0 indexed and we want to include both ends here). Then we either count up or count down, depending on which is higher.
tried it out but it didnt work sadly
– Christian LLanos
Nov 10 at 20:57
Can you say what it is that isn't working please? What result are you expecting that you aren't getting? I do believe the code above does what you asked for in your original question. Please confirm you're asking your question properly
– Capn Jack
Nov 10 at 20:59
so the range isn't working as it should, the response I'm getting back for the first on (2, 8) is giving me 0 at the start when it shouldnt. It similar to the second one also (18, 11). It starting at 11 when it shouldnt be.
– Christian LLanos
Nov 10 at 21:10
Did you copy the code exactly?? I'm running the exact same code and I'm getting the results you asked for.
– Capn Jack
Nov 10 at 21:12
yup copied the whole thing
– Christian LLanos
Nov 10 at 21:15
|
show 1 more comment
up vote
2
down vote
Firstly, what you're doing with your first two cases is kind of "cheating" and is what's known as "hardcoding" results. You're not writing a function to perform the task but instead you're just printing the answers you know you should be seeing.
Ironically though, you're pretty close here.
This should do what you want:
def range_of_numbers (num1, num2):
if(num1 < num2):
for i in range(num2-num1 +1):
print(num1+i)
elif(num1 > num2):
for i in range(num1-num2 +1):
print(num1-i)
else:
print(num1)
if either num1 or num 2 is greater, we find the difference and make a for loop of that size (+1 since we're 0 indexed and we want to include both ends here). Then we either count up or count down, depending on which is higher.
tried it out but it didnt work sadly
– Christian LLanos
Nov 10 at 20:57
Can you say what it is that isn't working please? What result are you expecting that you aren't getting? I do believe the code above does what you asked for in your original question. Please confirm you're asking your question properly
– Capn Jack
Nov 10 at 20:59
so the range isn't working as it should, the response I'm getting back for the first on (2, 8) is giving me 0 at the start when it shouldnt. It similar to the second one also (18, 11). It starting at 11 when it shouldnt be.
– Christian LLanos
Nov 10 at 21:10
Did you copy the code exactly?? I'm running the exact same code and I'm getting the results you asked for.
– Capn Jack
Nov 10 at 21:12
yup copied the whole thing
– Christian LLanos
Nov 10 at 21:15
|
show 1 more comment
up vote
2
down vote
up vote
2
down vote
Firstly, what you're doing with your first two cases is kind of "cheating" and is what's known as "hardcoding" results. You're not writing a function to perform the task but instead you're just printing the answers you know you should be seeing.
Ironically though, you're pretty close here.
This should do what you want:
def range_of_numbers (num1, num2):
if(num1 < num2):
for i in range(num2-num1 +1):
print(num1+i)
elif(num1 > num2):
for i in range(num1-num2 +1):
print(num1-i)
else:
print(num1)
if either num1 or num 2 is greater, we find the difference and make a for loop of that size (+1 since we're 0 indexed and we want to include both ends here). Then we either count up or count down, depending on which is higher.
Firstly, what you're doing with your first two cases is kind of "cheating" and is what's known as "hardcoding" results. You're not writing a function to perform the task but instead you're just printing the answers you know you should be seeing.
Ironically though, you're pretty close here.
This should do what you want:
def range_of_numbers (num1, num2):
if(num1 < num2):
for i in range(num2-num1 +1):
print(num1+i)
elif(num1 > num2):
for i in range(num1-num2 +1):
print(num1-i)
else:
print(num1)
if either num1 or num 2 is greater, we find the difference and make a for loop of that size (+1 since we're 0 indexed and we want to include both ends here). Then we either count up or count down, depending on which is higher.
answered Nov 10 at 20:29
Capn Jack
862520
862520
tried it out but it didnt work sadly
– Christian LLanos
Nov 10 at 20:57
Can you say what it is that isn't working please? What result are you expecting that you aren't getting? I do believe the code above does what you asked for in your original question. Please confirm you're asking your question properly
– Capn Jack
Nov 10 at 20:59
so the range isn't working as it should, the response I'm getting back for the first on (2, 8) is giving me 0 at the start when it shouldnt. It similar to the second one also (18, 11). It starting at 11 when it shouldnt be.
– Christian LLanos
Nov 10 at 21:10
Did you copy the code exactly?? I'm running the exact same code and I'm getting the results you asked for.
– Capn Jack
Nov 10 at 21:12
yup copied the whole thing
– Christian LLanos
Nov 10 at 21:15
|
show 1 more comment
tried it out but it didnt work sadly
– Christian LLanos
Nov 10 at 20:57
Can you say what it is that isn't working please? What result are you expecting that you aren't getting? I do believe the code above does what you asked for in your original question. Please confirm you're asking your question properly
– Capn Jack
Nov 10 at 20:59
so the range isn't working as it should, the response I'm getting back for the first on (2, 8) is giving me 0 at the start when it shouldnt. It similar to the second one also (18, 11). It starting at 11 when it shouldnt be.
– Christian LLanos
Nov 10 at 21:10
Did you copy the code exactly?? I'm running the exact same code and I'm getting the results you asked for.
– Capn Jack
Nov 10 at 21:12
yup copied the whole thing
– Christian LLanos
Nov 10 at 21:15
tried it out but it didnt work sadly
– Christian LLanos
Nov 10 at 20:57
tried it out but it didnt work sadly
– Christian LLanos
Nov 10 at 20:57
Can you say what it is that isn't working please? What result are you expecting that you aren't getting? I do believe the code above does what you asked for in your original question. Please confirm you're asking your question properly
– Capn Jack
Nov 10 at 20:59
Can you say what it is that isn't working please? What result are you expecting that you aren't getting? I do believe the code above does what you asked for in your original question. Please confirm you're asking your question properly
– Capn Jack
Nov 10 at 20:59
so the range isn't working as it should, the response I'm getting back for the first on (2, 8) is giving me 0 at the start when it shouldnt. It similar to the second one also (18, 11). It starting at 11 when it shouldnt be.
– Christian LLanos
Nov 10 at 21:10
so the range isn't working as it should, the response I'm getting back for the first on (2, 8) is giving me 0 at the start when it shouldnt. It similar to the second one also (18, 11). It starting at 11 when it shouldnt be.
– Christian LLanos
Nov 10 at 21:10
Did you copy the code exactly?? I'm running the exact same code and I'm getting the results you asked for.
– Capn Jack
Nov 10 at 21:12
Did you copy the code exactly?? I'm running the exact same code and I'm getting the results you asked for.
– Capn Jack
Nov 10 at 21:12
yup copied the whole thing
– Christian LLanos
Nov 10 at 21:15
yup copied the whole thing
– Christian LLanos
Nov 10 at 21:15
|
show 1 more comment
up vote
0
down vote
Use 1 or -1 as steps in your range:
def range_of_numbers(a, b):
if (a == b):
print("same")
else:
print(*list(range(a, b, (1 if a<b else -1))))
Test it:
range_of_numbers(2, 8)
range_of_numbers(18, 11)
range_of_numbers(42, 42)
Sure enough you get the desired output:
2 3 4 5 6 7
18 17 16 15 14 13 12
same
add a comment |
up vote
0
down vote
Use 1 or -1 as steps in your range:
def range_of_numbers(a, b):
if (a == b):
print("same")
else:
print(*list(range(a, b, (1 if a<b else -1))))
Test it:
range_of_numbers(2, 8)
range_of_numbers(18, 11)
range_of_numbers(42, 42)
Sure enough you get the desired output:
2 3 4 5 6 7
18 17 16 15 14 13 12
same
add a comment |
up vote
0
down vote
up vote
0
down vote
Use 1 or -1 as steps in your range:
def range_of_numbers(a, b):
if (a == b):
print("same")
else:
print(*list(range(a, b, (1 if a<b else -1))))
Test it:
range_of_numbers(2, 8)
range_of_numbers(18, 11)
range_of_numbers(42, 42)
Sure enough you get the desired output:
2 3 4 5 6 7
18 17 16 15 14 13 12
same
Use 1 or -1 as steps in your range:
def range_of_numbers(a, b):
if (a == b):
print("same")
else:
print(*list(range(a, b, (1 if a<b else -1))))
Test it:
range_of_numbers(2, 8)
range_of_numbers(18, 11)
range_of_numbers(42, 42)
Sure enough you get the desired output:
2 3 4 5 6 7
18 17 16 15 14 13 12
same
edited Nov 10 at 21:14
answered Nov 10 at 20:28
Ayxan
1,06715
1,06715
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53242986%2ffinding-the-range%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
So you have to return hard-coded values?
– Ayxan
Nov 10 at 20:16
'If both numbers are the same then it should stay the same' I'm not sure what you mean by this?
– smoggers
Nov 10 at 20:25
Yeah the values that were given, i have to return them. So i have to type a print statement that shows the numbers going up, one going down, and the last one that just the same
– Christian LLanos
Nov 10 at 20:40
basically for the sample it says 45, 45. Since these two are the same, the print statement has to be the same. However, if it came out like 2, 8, the number on the left is smaller than the one on the right, so it gonna increase going up. If it was the opposite (8, 2), the number is gonna decrease and go down.
– Christian LLanos
Nov 10 at 20:45
@ChristianLLanos What are your expected outputs for (2, 8), (18, 11) and (42, 42)? Which part of my answer's output is wrong?
– Ayxan
Nov 10 at 21:12