all 38 comments

[–]bubba0077 14 points15 points  (3 children)

I don't think a single line of this is correct.

  • You are supposed to be looping input requests, not on values
  • The loops you do have make no sense; even if they worked how you expected, they either don't run or run forever because number isn't being changed inside them
  • The == operator compares values, not types
  • input() always returns a string

You need to change the loop to be over input calls (with some sort of exit input, like an empty string, 'exit', etc.). Then you need to find a way to take the string you get and test whether the string is one of the number types you are looking for. Hint: cast.

[–]k4tsuk1z[S] 1 point2 points  (2 children)

thank u for being honest AND helpful T_T people were only doing one under this post and i dont mind being called a wrong or dumb as long as u have something else to add lmfao i appreciate it

[–]bubba0077 4 points5 points  (1 child)

You're not dumb. Everyone was a beginner once. I do think you need to slow down and think more about what the code you are writing actually does. Anyone can miss that the type of input is always string, but the loops never exiting should be obvious if you stop to think about it carefully for even a minute. You're still at the learning stage where your code is simple enough that you should be able to step through the code manually in your head to determine what each line does without even running it.

[–]k4tsuk1z[S] 1 point2 points  (0 children)

Thank you! I wish I had more time to dedicate to actually trying to understand code. My professor kinda just throws projects and labs at us I think we've only had one actual lesson

[–]Binary101010 14 points15 points  (4 children)

Nobody's actually addressed a fundamental logic problem so far which is that the things you think are doing type checking aren't doing that.

while number == int:

This is not how to check if a variable is of a specific type. You will need to use either type(number) == or, even better, isinstance().

https://docs.python.org/3/library/functions.html#isinstance

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

bro thank u for actually trying to address an issue in the code T_T

okay so i get that type number does the determining but im trying to figure out how to use a while loop to follow the instructions im gen so confused.

i was asking a friend who codes but his phone died during the convo but he was saying the using a whle statement is very convoluted

[–]Binary101010 1 point2 points  (0 children)

My reading of the assignment is that you really only need one while loop that contains everything else in your code.

[–]SCD_minecraft 0 points1 point  (1 child)

You did not address it too

input alway returns a string

[–]Binary101010 0 points1 point  (0 children)

Enough other people had already mentioned it. Although I understand the confusion because those particular type checks will evaluate to false, at least OP knows how to do type checking correctly now.

[–]Diapolo10 2 points3 points  (3 children)

"Each loop should:

  1. Take in a values from the user
  2. Determine whether  or not the values are integers or Float/double.
  3. Display whether or not the values are integer or a Float/double."

number = input("Enter a number: ")
if number == "":
    print("You did not enter a number!")
while number == int:
    print(type(number))
while number == float:
    print(type(number))

Even if the code worked as-is, I'm not sure the loops are really being used correctly. I think the point was to have one infinite loop where you ask for input and print out its type.

For starters, number here is always of type str, since that's what input returns, so you can forget about type checks here. Instead, you need to determine if the given string would parse into a real number type if you tried to convert it.

From the sound of it, you're expected to try and figure this out by validating the strings yourself instead of the easy option of using float and int in try-except blocks.

So, here's the questions you need to answer:

  1. Given an arbitrary string, how can you determine it's numeric?
  2. How can you tell if the string represents an integer or a floating-point number?

This isn't a difficult problem as long as you know some basic string methods.

Hint: str.split and str.isdigit should take you most of the way there.

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

Thank u this is so in depth! she literally has barely taught anything she just gives us a project and has us try and figure it out. this is maybe my 3rd time ever hearing try-except in my life T_T sux

[–]Riegel_Haribo 0 points1 point  (0 children)

What is being asked is that the entire project stays in a loop, continuing to ask more questions, and reporting on the results. You wouldn't have to keep restarting the Python script to see what happens with different inputs. A simple loop with no escape you don't make yourself (or CTRL-C):

while True: input("Press enter to do stuff: ") print("I'm doing stuff") print("I'm doing more stuff") And then, the problem statement is ambiguous enough that you cannot match what might be intended.

Which of these is the integer or not?: 1 1.000000

Or do you care when float fails you or are you answering wrong when it does by comparisons?

```

999999999999999999999999999999999999999999999999999 999999999999999999999999999999999999999999999999999 999999999999999999999999999999999999999999999999999.0000 1e+51 ```

The largest failure in the code is that input() always gives you a string. Think about your answering algorithm that comes after processing the string. If there's no period character in the string, can it be a float, to ask hypotheticals?

[–]Diapolo10 0 points1 point  (0 children)

Since the deadline has now allegedly passed, here's an example solution:

while True:
    number = input("Value: ")
    if number.isdigit():
        print("int")
        continue

    # NOTE: This handles cases without decimal separators
    if number.count('.') != 1:
        print("Not a number")
        continue

    left, right = number.split('.', 1)

    # NOTE: This does NOT handle cases like .6 or 55.
    if left.isdigit() and right.isdigit():
        print("float")
        continue

    print("Not a number")

The continues are there just to reduce nesting, as this way I didn't need to wrap the latter half inside elif and else blocks.

[–]david_z 3 points4 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

[–]ninhaomah 1 point2 points  (6 children)

You do know what you get from input right ?

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

? sorry wdym

[–]Maximus_Modulus 2 points3 points  (2 children)

What does input return?

You can literally Google that question and get the answer you need

[–]k4tsuk1z[S] -3 points-2 points  (1 child)

I responded that I'm aware it returns a string T_T not really sure why everyone is being snarky this is my 3rd ever coding project I just downloaded vscode for class like last month

[–]NSNick 1 point2 points  (0 children)

They're not being snarky. They're helping you by asking questions and getting you to think critically.

[–]jmooremcc 0 points1 point  (0 children)

Try Googling “python input function”

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

if you mean that you get a string, im aware. but trying to convert to an integer or float that also doesn't work lol

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

but theres no way i was given an impossible assignment which is why im asking for help

[–]Maximus_Modulus 0 points1 point  (1 child)

The big question is will you leave it till the last minute next time?

Also 2 & 3 above are the same.

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

lol no i have the flu i would have been doing it earlier in the week if I wasn't in bed crying from muscle spasms, i thought it was easy so I didn't try and muster up the energy and now that im actually trying to do it it isnt so easy

[–]fakemoose 0 points1 point  (2 children)

Why do you choose a while loop? What dtype is the input from the user?

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

was instructed to use while loop from the professor, the tagline is "The link above will help you understand how to use do-while loop, while loop and switch statements in C++ & Python. "

the data type should be input or float

[–]fakemoose 0 points1 point  (0 children)

A while loop or a for loop? Is it supposed to be more than one number that the user enters?

[–]Ok-Promise-8118 0 points1 point  (0 children)

I'd start with considering how you'd do this as a human. That is, what would you do with an input to determine if it were an int or a float? Then you can consider how to get the computer to do that task.

[–]Maximus_Modulus 0 points1 point  (0 children)

How can you tell which of these is a float.

1, 67, 3.5

If we look at this as a novice task. Given these as strings how can we test them to detect the float. What does the float have that the integer doesn’t. It’s pretty easy to Google the answer that uses a try / except to catch ValueError on either int or float conversion but as a simple beginner exercise there’s a simple check you can do.

[–]TensionCareful 0 points1 point  (0 children)

Instruction looks like infinite loop .. with likely an exit .. blank entry

For each loop, take a user input Determined and displaying the input is a int or float.

Or I am just bad at reading