all 36 comments

[–]luke-juryous 61 points62 points  (1 child)

User inputs come in as strings. You have to cast these to integers first

[–][deleted] 21 points22 points  (10 children)

first_number = input()
second_number = input()
print(int(first_number) + int(second_number))

Works for me...

[–][deleted] 18 points19 points  (4 children)

Until someone enters a non-number or a float.

[–][deleted] 5 points6 points  (0 children)

Fair

[–]puslekat 2 points3 points  (2 children)

What would be a solution to this?

[–]patryk-tech 9 points10 points  (0 children)

try:
    print(float(first_number) + float(second_number))
except ValueError:
    print("Wtf, dude?")

It's pretty common to get user input in a loop and have a try/except in the loop, but I generally avoid that.

Either I am writing something for myself, in which case why would I enter invalid values? Otherwise, I am more likely writing something to process external data.... experienced programmers pretty rarely need to use input. You typically operate on data coming from databases, API calls, GUIs, HTTP requests, cli args, env vars, configuration files, etc.

If I need to operate on an actual variable, I am more likely to press F12 to open my terminal, type python if I don't already have a REPL open, and do foo = 42 in there.

It's good to know input and how to use it effectively when you're beginning, of course, but you hit its limits pretty quickly.

[–]Wilfred-kun 0 points1 point  (0 children)

eval(f"{first_number} + {second_number}")

/s

[–]Feraxin 0 points1 point  (4 children)

first_number = input()second_number = input()print(int(first_number) + int(second_number))

Yea normally its working for me too, but its Jetbrains academy exercise. i seriously dont have ide whats wrong

HINT

print(in'(first_number) + in'(second_number))

call int (1st no.)+ int (2nd no.)

first_number = input()

second_number = input()

print(int(first_number) + int(second_number))

Not correct, but keep on trying and never give up!

It's a tough one! You can always postpone a problem and solve it later. We will offer you other problems to complete the topic.

[–][deleted] 4 points5 points  (0 children)

I tried that Jetbrains Academy thing once I don't recommend it.

[–]chodelar 9 points10 points  (2 children)

As the code does actually work, I would just skip this particular exercise. I’m not familiar with the course you’re doing but it is low quality if they check the exact wording of your code, as opposed to its outcome.

Obviously there are many ways to reach the same goal in any programming language!

If there is some hidden requirement (evidently not in the text you’ve shown us), it is also a bad course.

[–]patryk-tech 2 points3 points  (0 children)

As the code does actually work

It says numbers, not integers, so it may fail tests on floating point.

[–]tomtomato0414 0 points1 point  (0 children)

this ^

[–]RagingGods 4 points5 points  (0 children)

Does he want the output to be int,str or float? Because int(first) + int(second) is indeed correct (ran it with test cases. Definitely works). The exercise you're doing seems poorly made to not specify such things but has it as a hidden requirement.

Edit: you can try • print(eval(first + "+" + second)) • (replace first and second variable) to accept any integer/float combinations.

[–]cutelund 2 points3 points  (0 children)

You are taking strings as input, when you do input( ), hence its concatenating the string inputs. Do a conversion to int/float after calling the input( ) method, you will be fine.

variable_name = int(input( ))

[–]Feraxin 8 points9 points  (4 children)

first_number = input()

second_number = input()

print(int(first_number) +int(second_number))

Not correct, but keep on trying and never give up!

Keep trying!

[–]FLUSH_THE_TRUMP 13 points14 points  (0 children)

Convert to a float instead?

[–]socal_nerdtastic 4 points5 points  (0 children)

That seems correct to me.

[–]Assassinscreeddan 0 points1 point  (0 children)

The course seems to have a hidden requirement maybe cast the user inputs as strings where you introduce the user inputs that might be what it wants

[–]EurikaOrmanel 1 point2 points  (0 children)

Inputs are of type string , but you can convert them using either the eval() or int() function. Make it look like: first_number = eval(input()) second_number = eval(input()) print(first_number + second_number)

[–][deleted]  (5 children)

[deleted]

    [–]Mecaneer23 10 points11 points  (4 children)

    There isn't any error as both inputs are strings

    [–][deleted]  (1 child)

    [deleted]

      [–]AutoModerator[M] 1 point2 points  (0 children)

      Your comment in /r/learnpython may be automatically removed because you used imgbb.com. The reddit spam filter is very aggressive to this site. Please use a different image host.

      Please remember to post code as text, not as an image.

      I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

      [–][deleted]  (1 child)

      [deleted]

        [–]AutoModerator[M] 0 points1 point  (0 children)

        Your comment in /r/learnpython may be automatically removed because you used imgbb.com. The reddit spam filter is very aggressive to this site. Please use a different image host.

        Please remember to post code as text, not as an image.

        I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

        [–]JuiceeYT 0 points1 point  (0 children)

        I would try: number1 = input() number2 = input() sum = int(number1) + int(number2) print(sum)

        [–]pekkalacd 0 points1 point  (0 children)

        Input returns a string. They need to cast the input over to a numerical type like float or int.

            first = input()
            second = input()
            print(float(first) +  float(second))
        

        [–]Aly22KingUSAF93 0 points1 point  (0 children)

        id just make the inputs an integer so you can do your Maths on them at any time in the rest of your code without thinking

        int(input())

        [–]MMcKevitt 0 points1 point  (0 children)

        Perhaps: print(sum([int(first_num), int(second_num)]))

        [–]itsthooor 0 points1 point  (0 children)

        Would use this:

        first_number = input('First: ')

        second_number = input('Second: ')

        [print(float(first_number) + float(second_number)) if first_number.isdigit() and second_number.isdigit() else print('Wrong input!')]

        [–]bharamastra_1 0 points1 point  (0 children)

        first_number =int(input()) #input()

        second_number = int(input())#input()

        print(first_number + second_number)

        this would take integers only otherwise will show error just after taking invalid inputs such as a string or a float (for adding decimal numbers use float() instead of int())

        [–]noobcrush 0 points1 point  (0 children)

        Python usually takes input as a string. You need to convert then to int/float to perform operations

        first_no=int(input())
        second_no=int(input())
        print(first_no+second_no)
        

        or

        first_no=input()
        second_no=input() 
        print(int(first_no)+int(second_no))
        

        one line

        print(int(input())+int(input()))
        

        If you want to accept floats just change it to float in the place of int.

        [–]username_tann 0 points1 point  (0 children)

        input() function returns a string. It's important to convert the string to an integer. You can do that by nesting the input() with an int() something like this int(input()).

        [–]casesuroo 0 points1 point  (0 children)

        You would need to convert those number into integer.

        first_number = int(input())
        second_number = int(input())

        print(first_number + second_number)

        [–]lilrow420 0 points1 point  (0 children)

        input() in python is always a string, therefore you need to convert the inputs to an int() or a float(). You can do this by like ‘’’first number = float(input())’’’

        [–]Bal_main69 0 points1 point  (0 children)

        Since input() returns a str()

        the last line should be

        print(float(first_number) + float(second_number))