这个问题在这里已经有答案:
How can I read inputs as numbers?(10个答案)
22天前关门了。
所以当我运行这个程序时,它运行得很好,直到它到达变量‘t’的输入。当我输入0.75作为输入时,它给出一个错误。然而,如果我去掉输入函数,只把t=0.75,程序就能完美地工作。为什么会发生这种情况,我如何修复它?
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)
1条答案
按热度按时间mklgxw1f1#
input()
返回字符串,您需要将其转换为数字:float()
将字符串转换为浮点数(实数)如果需要输入整数,可以使用
int()