I just started learning python so please use newbie stuff thanks! I am trying to make a code that you input a number to start, to end and how many numbers the program counts up in and I want it to be integers and not float numbers but I can’t figure it out and I don’t understand what the error means. Please help!
This is my code:
start = input ("What number would you like the program to start counting at? : ")
isfloat1 = isinstance(start)
if isfloat1 == True :
end = input ("What number would you like the program to stop counting at? : ")
isfloat2 = isinstance(end)
if isfloat2 == True :
First of all, have you tried to run this code and looked at the error message, e.g. in the Python console?
if you look up the docs of isinstance you’ll notice that it takes two parameters: the object you want to test and the type you want to test it against.
However, that is not going to help immediately as input always returns a string.
What you can do instead is convert input’s result to a number, then you can use it in counting. You can just convert to integer if you only want to use that, and ask for new input if it’s not
This makes no sense at all. First, you cannot just ask whether start “is an instance”. You need to specify the of what part. Did you want to know if it’s an int? A float? A string? Something else?
Second, this is a pointless question to ask, because startwill be a string, no matter what the user typed.
Floating-point numbers are completely irrelevant here. When you prompt me with input ("What number would you like the program to start counting at? : "), nothing prevents me from typing seven, or .-~*~-., etc.
To find out whether the input was acceptable for converting to an integer, the right way is to try it, and then decide what to do in the exceptional case.
This does not changestart. It creates a new value that is an integer equivalent of start, which is then immediately thrown away.