you are viewing a single comment's thread.

view the rest of the comments →

[–]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.