What are blank float inputs defined as? Clearly not zero

When I put nothing into a float input, instead of it returning “num is 0” it returns as completely blank. (I made the code in 30 seconds to demonstrate the problem, and I am relatively new to coding.)

num = float(input("enter a number"))
if (num >= 1):
  print("num is above 1")
elif (num <= -1):
  print("num is below -1")
elif (num == 0):
  print("num is 0")

A blank input is treated as an empty string.

But it’s a syntax error to convert an empty string to a float, so your code doesn’t even run.

Did you copy your code correctly?

What I am trying to do is return text saying something like “not a number from 1 - 5” if it is below or above five, but I don’t know what blank is defined as so I am stuck there, and if there is any errors, it may be because i have no idea how to format text.

Often the right thing to do is to attempt to convert it to a number and catch the error if that fails.

Something like the following:

try:
    num = float(input("put a number "))
except ValueError:
    print(f"ERROR: Did not find a valid number")
    exit()
...

After the except section, you know that num is at least a valid float() and you can compare it against your endpoints.

2 Likes

Hmm, that works with text, but not necessarily blanks.

Note: I am using Trinket to code because that is what I am using for a project, so it may be a bug with trinket, and not an actual bug with python, and another thing to consider is that I am not actually receiving an error, my code just no longer continues.

Can you give more information on why it wouldn’t work with blanks? Any input that float() doesn’t like (empty string, spaces, random text) should raise a ValueError and go to the except stanza.

What sort of input is “blanks”? Is that someone just hitting return or something else?

I tried making a trinket with your code and it behaves as I expect. I think you’d need to describe what the input is, what the program is doing, and how that differs from what you expected the program to do.

You might want to put an “I’m done” print at the end of your code. It’s possible for some inputs (like 0.5) for it to print nothing (because that matches none of your if conditionals and there’s no output if they are bypassed).

To answer your question, the code continues, I don’t know what nothing is in terms of float values.

Im referring to blanks as in nothing, just pressing enter when you get the input. What I am actually working on is below, the bug is the input after it prints “1 for pentagon, 2 for square, 3 for triangle, and 4 to clear.”:

import turtle


def grid():
  global area, red, green, blue, area_2, shape
  turtle.shape("circle")
  print('Creating grid...')
  turtle.speed(5000)
  turtle.color('#000000')
  turtle.goto(-1000,0)
  turtle.goto(1000,0)
  turtle.goto(0,0)
  turtle.goto(0,1000)
  turtle.goto(0,-1000)
  turtle.goto(0,0)
  turtle.color('#999999')
  for count in range(10):
    turtle.forward(20)
    move()
  turtle.goto(0,0)
  for count2 in range(10):
    turtle.backward(20)
    move()
  turtle.goto(0,0)
  turtle.right(90)
  for count3 in range(10):
    turtle.backward(20)
    move()
  turtle.goto(0,0)
  for count4 in range(10):
    turtle.forward(20)
    move()
  turtle.color('#000000')
  turtle.penup()
  turtle.goto(0,0)
  turtle.pendown()
  turtle.left(90)
  turtle.goto(-1000,0)
  turtle.goto(1000,0)
  turtle.goto(0,0)
  turtle.goto(0,1000)
  turtle.goto(0,-1000)
  turtle.goto(0,0)
  print('Done!')
  print('')
  turtle.color('#000000')
  ask()


def move():
  global area, red, green, blue, area_2, shape
  turtle.right(90)
  turtle.forward(200)
  turtle.backward(400)
  turtle.forward(200)
  turtle.left(90)

def text_prompt(msg):
  try:
    return raw_input(msg)
  except NameError:
    return input(msg)


def ask():
  global area, red, green, blue, area_2, shape
  turtle.penup()
  area = float(text_prompt('Enter a value from -10 - 10 for [X] value'))
  while area > 10:
    print('Number is not inbetween -10 and 10')
    print('')
    area = float(text_prompt('Enter a value from -10 - 10 for [X] value'))
  while area < -10:
    print('Number is not inbetween -10 and 10')
    print('')
    area = float(text_prompt('Enter a value from -10 - 10 for [X] value'))
  turtle.speed(1.1)
  turtle.goto(area * 20,0)
  area_2 = float(text_prompt('Enter a value from -10 - 10 for [Y] value'))
  while area_2 > 10:
    print('Number is not inbetween -10 and 10')
    print('')
    area_2 = float(text_prompt('Enter a value from -10 - 10 for [X] value'))
  while area_2 < -10:
    print('Number is not inbetween -10 and 10')
    print('')
    area_2 = float(text_prompt('Enter a value from -10 - 10 for [X] value'))
  turtle.goto(area * 20,area_2 * 20)
  turtle.pendown()
  red = float(text_prompt('Enter a value from 0 - 255 for [RED] value'))
  blue = float(text_prompt('Enter a value from 0 - 255 for [BLUE] value'))
  green = float(text_prompt('Enter a value from 0 - 255 for [GREEN] value'))
  print('')
  print('1 for pentagon, 2 for square, 3 for triangle, and 4 to clear.')
  shape = float(text_prompt(''))
  while shape > 5:
    print('Number is not inbetween  1 and 5')
    print('')
    shape = float(text_prompt(''))
  while shape < 1:
    print('Number is not inbetween  1 and 5')
    print('')
    shape = float(text_prompt(''))
  if shape == 1:
    pentagon()
  elif shape == 2:
    square()
  elif shape == 3:
    triangle()
  elif shape == 4:
    clear()
  elif shape == 5:
    clear2()

def colour_rgb(r, g, b):
  r = round(min(100, max(0, r)) * 2.55)
  g = round(min(100, max(0, g)) * 2.55)
  b = round(min(100, max(0, b)) * 2.55)
  return '#%02x%02x%02x' % (r, g, b)


def pentagon():
  global area, red, green, blue, area_2, shape
  turtle.color(colour_rgb(red, green, blue))
  turtle.shape("circle")
  turtle.speed(10)
  turtle.begin_fill()
  turtle.right(72)
  for count5 in range(5):
    turtle.forward(-40)
    turtle.right(72)
  turtle.end_fill()
  turtle.left(72)
  ask()


def square():
  global area, red, green, blue, area_2, shape
  turtle.color(colour_rgb(red, green, blue))
  turtle.shape("circle")
  turtle.speed(10)
  turtle.begin_fill()
  turtle.right(90)
  for count6 in range(4):
    turtle.forward(-60)
    turtle.right(90)
  turtle.end_fill()
  turtle.left(90)
  ask()


def triangle():
  global area, red, green, blue, area_2, shape
  turtle.color(colour_rgb(red, green, blue))
  turtle.shape("circle")
  turtle.speed(10)
  turtle.right(120)
  turtle.begin_fill()
  for count7 in range(3):
    turtle.forward(-60)
    turtle.right(120)
  turtle.end_fill()
  turtle.left(120)
  ask()


def clear():
  global area, red, green, blue, area_2, shape
  turtle.speed(5000)
  turtle.color('#ffffff')
  turtle.penup()
  turtle.goto(-1000,-1000)
  turtle.pendown()
  turtle.begin_fill()
  turtle.goto(-1000,1000)
  turtle.goto(1000,1000)
  turtle.goto(1000,-1000)
  turtle.end_fill()
  turtle.penup()
  turtle.speed(5)
  turtle.goto(0,0)
  turtle.pendown()
  turtle.color('#000000')
  grid()
  ask()


def clear2():
  global area, red, green, blue, area_2, shape
  turtle.speed(5000)
  turtle.color('#ffffff')
  turtle.penup()
  turtle.goto(-1000,-1000)
  turtle.pendown()
  turtle.begin_fill()
  turtle.goto(-1000,1000)
  turtle.goto(1000,1000)
  turtle.goto(1000,-1000)
  turtle.end_fill()
  turtle.penup()
  turtle.speed(5)
  turtle.goto(0,0)
  turtle.pendown()
  turtle.color('#000000')
  ask()


grid()

Do you understand what the try/except stuff does in the example? It works just fine with blanks. (where “works” means that it detects that no number has been entered).

You’d need to do something similar with your main code.

When I pasted in

try:
    num = float(input("put a number "))
except ValueError:
    print("ERROR: Did not find a valid number")
    exit()
print("Done")

and pressed enter, it did not print the except, and skipped to the “Done”, try putting it into a trinket (not python 3.0) and seeing what I mean.

This seems like a trinket thing. For python2 and python3, an empty string is normally a ValueError that must be caught.

Python 2.7.5 (default, Jun 20 2023, 11:36:40)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> float("")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: could not convert string to float:

But in trinket, it converts to nan. You could check if you got that float value, or you could check the input for an empty string ahead of time.

>>> float("")
nan

(I didn’t realize that the trinket I created earlier was python3, that’s why it worked).

1 Like

I think trinket’s old Python is some messed up Javascript pseudo-Python. Try import random; print random.random, it prints <function <native JS>> (weirdness with its random module is how I first encountered trinket). Their Python 3 instead seems to be regular CPython.

1 Like

Thanks, that solved my problem! I used

  elif math.isnan(shape):
    print("Not a number from 1 - 5")
    ask()

(Ask just restarts it, and shape is a number.)
I thought that blank inputs were 0, because apparently NaN, NaN, NaN in RBG format is black.