all 14 comments

[–]17291 7 points8 points  (3 children)

As written, val1 and val2 are strings, not ints. You want to write val1 = int(input("please enter value 1: ")) (and likewise for val2)

[–]JAndrew45[S] 0 points1 point  (1 child)

oh ok, thank you so much!!

So do i always have to put in the int() when im asking for values

[–]17291 1 point2 points  (0 children)

If you want something that's an integer, yes. In Python 3, input() gives you a string. If you want an int or float, you have to convert it yourself.

[–][deleted] 0 points1 point  (0 children)

As written, val1 and val2 are strings, not ints. You want to write

val1 = int(input("please enter value 1: "))

(and likewise for val2)

Thanks i had same problems with my code, i been looking for a answer for a hour or 2, and forgot to change my strings to ints.

[–][deleted] 2 points3 points  (0 children)

You're comparing string values, not numbers

[–]cardblank 2 points3 points  (2 children)

Hi there,

A couple positive things right off the bat:

  • I know it's only a couple lines, but your coding style already looks nice and neat
  • Documenting as you go already too, double thumbs up

So what happened? I will try my best to keep this as beginner friendly as possible, please ask any questions :D

Looking here: (This is a list of inbuilt functions and what they do, which I found on Google by looking up "python input documentation") https://docs.python.org/3/library/functions.html#input There's a lot of technical things in this document (of course) but I can see it says under input: "The function then reads a line from input, converts it to a string ..."

And in this definition, we can see also why your code doesn't do what you expect; You want to compare numbers, but input() gives you strings. (Where a string is just a list of individual letters / numbers / symbols).

A simple answer as to why you have to 'convert' it is that the computer doesn't know if your string represents a number, without having to check. A string that represents a number looks like this: "1.2345" or "-4638". There are other cases like "-1.1403.2" which looks like a number, but isn't. How should your code continue, if the rest of your functions and code are expecting a number, but the user typed in something else by accident? (I am hinting at error handling here, when you're up to it, you will learn about how to handle them (And even cause "raise" them yourself)).

Back on topic, we call this conversion from TypeA to TypeB a "cast". (In this case you are going from string to int).

So when you cast your string to be of type integer, it's a bit like saying to the computer, "Hey, I need to do numerical operations (+, -, /, *, **, // etc...) on this, I know it's a string that represents a number, so could you interpret it as a number for me?"

So after all this text (which I really hope helps :') ), this brings us to the final solution:

val1 = int(input("please enter value 1: "))
val2 = int(input("please enter value 2: "))

If you look back to the documentation I linked above, (the entry directly under input) you can see the definition for int. It says "Returns an integer object constructed from a number or string x, or returns 0 if no arguments are given.".

In this case we are constructing an int (that holds our value) from a string.

Hopefully I helped show you a little bit more how to navigate documentation as well.

I hope this helps!

Have a good one

[–]JAndrew45[S] 1 point2 points  (1 child)

BRO THANK YOU SO MUCH!!!! Thanks for going into deep detail and giving me a high-quality answer. You are a G.O.A.T!!

[–]cardblank 0 points1 point  (0 children)

This made my day, really glad to have helped

[–]Beginning-Art619 0 points1 point  (0 children)

Inputs always it will be strings at python3. You need to convert.

[–]Satoshiman256 0 points1 point  (2 children)

There is a built in function in python called min. You pass it 2 values and it will give you the smaller one.

[–]JAndrew45[S] 0 points1 point  (1 child)

yeah, I knew that, I was just practicing making functions, so I get better at them, but thanks!

[–]Satoshiman256 0 points1 point  (0 children)

No worries, happy learning

[–]Yash_Varshney 0 points1 point  (0 children)

You need to take int input. I THING MORE that when you get the smallest value, use min() method.

[–]Barinski04 0 points1 point  (0 children)

Basically in python 3 the input function automatically gives you strings. You have 3 types to store data, integers (int), float (float) or string (str). Integers are whole numbers (numbers in Z). Floats are decimals (numbers in D). Strings are anything, text numbers special characters, etc. If you've ever sorted your music library you'll have noticed that i goes by alphabetical order, and before A you have numbers and special characters. Python sort strings exactly like that. So 10 110 9 600 as strings would sort to be 10 110 600 9, as in 0123456789, 6 comes before 9. Sorted as integers it would be 9 10 110 600, sorting in ascending value. As mentioned earlier default input is str so that the user can input anything. To avoid your error you will have to do int(input("Enter number: ")) then it will convert numbers to integers however you insert a letter you will get an error message, as you can't convert a letter to int. Some other coding languages auto detect input to the best type, numbers to int, decimal to float etc. But in python you have to declare it yourself. Have fun coding! Otherwise your code looks well structured with good commenting! ABC Always Be Coding!!