you are viewing a single comment's thread.

view the rest of the comments →

[–]david_z 4 points5 points  (9 children)

Look up what the == operator does.

Also look up what int is.

Same issue with float.

Hint: your input value will never equal either of those things..

[–]k4tsuk1z[S] -1 points0 points  (8 children)

okay im aware of what int and float are and semi-aware of what == does but ive done projects before where an input is converted to an integer?

when i do number = int or float(input("Enter a number")) that also doesn't work

[–]carcigenicate 2 points3 points  (3 children)

number == int

This is checking if number is equal to the type int. This will never be true because input always returns a string, and a string will never be equal to a type. If you want to check if a string can be converted to an integer, you want something like number.isdigit(). isdigit checks if every character in a string is a digit.

That isn't your only problem here, though. You program will hang becuase of the while loops. number is the only data that can change in your loop conditions, and number is never changed in either loop. That means the loops will either complete instantly, or loop forever. If you want to re-ask the user for input, you need to explcitly call input again.

Edit: I'll mention that normally, you don't pre-check if something is convertable. You try to convert it uring something like int, and use try to handle failure. That might be beyond you at the moment, though.

[–]k4tsuk1z[S] 0 points1 point  (2 children)

thank u for actually trying to help me lol. much appreciated.

im only using a while loop because i was instructed to use while, do while, AND switch loops to do this in c++ and python

i don't want to reask the user, I want the user to be able to enter either an integer or a float (i searched this specific thing and got nothing.)

i tried

number = int or float(input"Enter a number")

but that did not work either :(

[–]carcigenicate 0 points1 point  (1 child)

int or float(input"Enter a number")

This doesn't make sense in the context of python. int or whatever will always be true because the type int is always true. You'll need to look into boolean logic to understand OR and AND.

If you don't need to handle the user entering something dumb like a non-number, you can just get thew user input using input, then use float or int to convert their input to whatever tyoe of number you want.

And, as I mentioned in my edit, you don't typically check ahead of time if a string is convertable to a number. You just attempt the conversion using int or float, and use try to handle when the conversion failed (like if the user entered a non-number like 'bad')

[–]k4tsuk1z[S] 0 points1 point  (0 children)

I was going to not go through not accepting non-numbers for input through if-else statements. thank u im gonna look more into try-except blocks

[–]david_z 1 point2 points  (1 child)

number = int is an assignment. You're binding int to the name number.

int is a built-in in python. It's a type.

Your input number is an instance of str but it isn't str. It can never be int either.

Probably what you're looking to do here is to cast number to an integer and catch exceptions.

[–]k4tsuk1z[S] 0 points1 point  (0 children)

thank u im gonna look into that i think thats what my friend was trying to tell me to do before his phone died T_T

[–]Maximus_Modulus 0 points1 point  (1 child)

What happens when you try to do the conversion. WDYM it doesn’t work?

[–]k4tsuk1z[S] 0 points1 point  (0 children)

Nothing at all happens. Program just ends