-1

We were given an exercise where a user must input two numbers and the output must be the numbers between the two given numbers, with the condition that the only input are numbers, and if the user types anything besides that will print "INVALID INPUT!"

Here is what I have tried:

num1 = int(input('Give me a #:'))

num2 = int(input('Give me another#:'))


if num1>num2

print("First number should be lesser than second number")


elif num1<num2

print(list(range(num1,num2)))


else:

print("Invalid Input")
wjandrea
  • 28,235
  • 9
  • 60
  • 81
Rick V
  • 23
  • 1
  • 8
  • 2
    Also, a colon is missing after ```if``` ```:``` –  Jul 22 '21 at 03:42
  • 2
    Just an extra point. Make sure to indent the print lines properly after your conditional statements. Like the answer below. – vijay v Jul 22 '21 at 03:50
  • 2
    What's the error or the question? Just posting your code and etc.. doesn't work. You should tell us the problem or question. – PCM Jul 22 '21 at 03:51
  • 1
    Welcome to Stack Overflow! Please take the [tour] and read [ask]. What's your question? Is it about the syntax errors in your code? Please [edit] to clarify. See also [How do I ask and answer homework questions?](https://meta.stackoverflow.com/q/334822/4518341) and [mre]. – wjandrea Jul 22 '21 at 04:23
  • @Robin [There's no consensus about a preferred code formatting style.](https://meta.stackoverflow.com/q/387765/4518341) Either way, your edit makes no change to the actual appearance of the post, so it's pointless. – wjandrea Jul 22 '21 at 04:28
  • @Rick V, That was a good question. The fact that the initial number cannot be included was a nice twist. I voted your question up. Those who voted it down didn't catch that and thought yours was just a basic unresearched question. – Robin Sage Jul 22 '21 at 04:39
  • Yep, it finally worked, I guess I need to master the language more I'm new to python so yeah.... – Rick V Jul 22 '21 at 04:40
  • If you found my answer useful, please vote it up and check it as answered. – Robin Sage Jul 22 '21 at 04:41

3 Answers3

2

You should try this. When you try to convert anything other than the number to an integer, a ValueError is raised. You can catch that to print the error

while True:
    try:
        num1 = int(input('Give me a #:'))
        num2 = int(input('Give me another#:'))
        if num1>num2:
            print("First number should be lesser than second number")
        elif num1<num2:
            print(list(range(num1+1,num2)))
            break
    except ValueError:
        print("Invalid Input")
  • Sorry for the vote down. While your code does work, it still includes the first number in the output. – Robin Sage Jul 22 '21 at 04:19
  • @RobinSage, I cannot get you? –  Jul 22 '21 at 04:20
  • Sorry I don't understand the question. – Robin Sage Jul 22 '21 at 04:21
  • @RobinSage, *While your code does work, it still includes the first number in the output.*? –  Jul 22 '21 at 04:22
  • Oh I see. If you run your code, it includes the first number in the range. @Rick V asked that only the numbers between the chosen numbers be printed out. For example, the user chooses 2 and 6, so the print out must be [3,4,5]. But your code includes 2 also. – Robin Sage Jul 22 '21 at 04:24
  • ```range()``` function doesn't work that way in python @RobinSage, it prints the numbers from starting element to ending-element - 1 –  Jul 22 '21 at 04:26
  • I know how range() works. All I'm saying is that the requirement was that only the numbers between two chosen numbers be printed. – Robin Sage Jul 22 '21 at 04:28
  • @RobinSage done. Addressed the issue –  Jul 22 '21 at 04:30
  • I voted up. YOu can return the favor.. :) – Robin Sage Jul 22 '21 at 04:33
  • 1
    @RickV Most welcome! Consider accepting and upvoting the answer :-) –  Jul 22 '21 at 04:42
2
num_list = [] # create a list to append all numbers between user's chosen numbers
while True:
    try:
        num1 = int(input("Enter first number: "))
        num2 = int(input("Enter second number: "))

        for i in range(num1+1, num2): # iterate over the range between first number and second number. Add 1 to num1 so that num1 is not included in list
            num_list.append(i) 

        print(f'First number: {num1}n\Second number: {num2}')
        print(f'The numbers between {num1} and {num2} are:\n{num_list}')
        break
    except:
        print("Input must be a number. Try again.")
Robin Sage
  • 969
  • 1
  • 8
  • 24
0

You have a Syntax error, please add a colon : after if and else conditions like

if condition:
    pass
elif condition2:
    pass
else:
    pass
Dhruv Agarwal
  • 558
  • 6
  • 15
  • 2
    You should have commented this instead of answering. We are not sure if the colon is in the real code but not in the formatted stack overflow question – PCM Jul 22 '21 at 04:09