--- title: 'Python Challenge Day 6 - Find out whether the shape is a cube' disqus: hackmd --- ## Python Challenge Day 6 - Find out whether the shape is a cube ### DESCRIPTION To find the volume (centimeters cubed) of a cuboid you use the formula: volume = Length * Width * Height But someone forgot to use proper record keeping, so we only have the volume, and the length of a single side! It's up to you to find out whether the cuboid has equal sides (= is a cube). Return true if the cuboid could have equal sides, return false otherwise. Return false for invalid numbers too (e.g volume or side is less than or equal to 0). Note: side will be an integer 程式邏輯 --- 目的:計算長方體的體積(立方厘米,體積=長 * 寬 * 高) 已知體積和單邊的長度,判斷長方體是否具有相等的邊(= 正方體)。如果長方體邊相等,則返回 true,否則返回 false。 例外:對於無效數字也返回 false(例如體積或邊小於/等於 0) 注意:邊將是一個整數 1. 如果volume(體積)等於0,代表有一邊長為0,所以 if volume=0 return false 2. 如果volume(體積)小於0,代表有一邊長為負數,所以 if volume<0 return false 3. 判斷side(邊),長寬高是否相等 假如長寬高都是2 cm,那體積則是2 * 2 * 2,也就是2的3次方 因此volume(體積) = side(邊)3次方,如果體積不等於邊的3次方,那代表3個邊長不相等 if volume = side ** 3 return true else return false Solution --- ``` python def cube_checker(volume, side): if side <=0 or volume <=0 or side ** 3 != volume: return False else: return True ``` * 使用 def 關鍵字來定義函數(cube_checker) * 當side <=0 or volume <=0 or side ** 3 != volume 回傳False ## 參考資源 :::info * [Kata](https://www.codewars.com/kata/58d248c7012397a81800005c) :::