0

So when I run this program, it runs fine until it gets to the input for variable 't'. When I enter 0.75 as the input it gives an error. However, if I remove the input function and just put t = 0.75 the program works perfectly. Why is this happening and how do I fix it?


import matplotlib.pyplot as plt
import numpy as np


file = plt.imread(input('Enter file name: '))   #Read in image
countSnow = 0            #Number of pixels that are almost white
t = input("Enter threshold: ")                 #Threshold for almost white-- can adjust between 0.0 and 1.0

#For every pixel:
for i in range(file.shape[0]):
     for j in range(file.shape[1]):
          #Check if red, green, and blue are > t:
          if (file[i,j,0] > t) and (file[i,j,1] > t) and (file[i,j,2] > t):
               countSnow = countSnow + 1
                  
print("Number of white pixels:", countSnow)

Ryan
  • 51
  • 4

1 Answers1

1

input() returns string and you needs to turn it into a number:

# ...
t = float(input())
# ...

float() converts string to floating point number (real number)

If you would need to input whole number, you could use int()

Aiq0
  • 306
  • 1
  • 11