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.










share|improve this question
























  • 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

















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.










share|improve this question
























  • 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















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.










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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




















  • 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














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.






share|improve this answer





















  • 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


















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





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    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

























    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.






    share|improve this answer





















    • 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















    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.






    share|improve this answer





















    • 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













    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.






    share|improve this answer












    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.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    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


















    • 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












    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





    share|improve this answer



























      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





      share|improve this answer

























        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





        share|improve this answer














        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






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 10 at 21:14

























        answered Nov 10 at 20:28









        Ayxan

        1,06715




        1,06715






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Florida Star v. B. J. F.

            Error while running script in elastic search , gateway timeout

            Adding quotations to stringified JSON object values