Python 将函数读取input()为字符串。在将它传递给我的函数进行除法之前,变量使用int(). 如果一个变量是非整数(例如"a"),那么如何捕获它?
def divideNums(x,y):
try:
divResult = x/y
except ValueError:
print ("Please provide only Integers...")
print (str(x) + " divided by " + str(y) + " equals " + str(divResult))
def main():
firstVal = input("Enter First Number: ")
secondVal = input("Enter Second Number: ")
divideNums (int(firstVal), int(secondVal))
if __name__ == "__main__":
main()
如何处理firstVal/ 的类型转换secondVal?
一只名叫tom的猫
森栏
相关分类