問題出在「你沒貼出來的程式碼」
原因是
pythone 的變數型態「預設」是 str
如果沒宣告成自己定義的物件,就會出現該錯誤訊息
以下是錯誤和正確的寫法供參考
錯誤版
正確版
最簡單的改法
就是在最前面,宣告所有變數,後面就都不必寫了
大概像這樣
price11 = 350
price21 = 250
price31 = 1000
price41 = 850
price51 = 250
shirt = shirt()
Tshirt = Tshirt()
pants = pants()
suitskirt = suitskirt()
blouse = blouse()
附上整串謝謝
class clothes:#父類別都是衣服
def init(self, radius= '0', volume = '0'):
pass#繼承大家都有得避免程式重複
#子類別男士襯衫
class shirt(clothes):
#材質和價格
def set_mp1(self,material= '男士襯衫', price11= 350):
self.material = material
self.price11 = price11
print(material, ',共計', price11,'元')
#子類別男士運動衫
class Tshirt(clothes):
#材質和價格
def set_mp1(self, material= '男士運動衫', price21= 250):
self.material = material
self.price21 = price21
print(material, ',共計', price21,'元')
#子類別男士西裝褲
class pants(clothes):
#材質和價格
def set_mp1(self, material= '男士西裝褲', price31= 1000):
self.material = material
self.price31 = price31
print(material, ',共計', price31,'元')
class suitskirt(clothes):
#材質和價格
def set_mp1(self, material= '女士西裝', price41= 850):
self.material = material
self.price41 = price41
print(material, ',共計', price41,'元')
class blouse(clothes):
#材質和價格
def set_mp1(self, material= '女士襯衫', price51= 250):
self.material = material
self.price51 = price51
print(material, ',共計', price51,'元')
price11 = 350
price21 = 250
price31 = 1000
price41 = 850
price51 = 250
adjust = input('男士襯衫(shirt)一件:350元,男士運動衫(Tshirt)一件:250元,男士西裝褲(pants)一件:1000元,女士西裝裙(suit skirt)一件:850元,女士襯衫(blouse)一件:250元,有要修改單價(y/n):')
while adjust == 'y':
ad_type = input('請問修改哪一種衣服價格(1. 男士襯衫、2. 男士運動衫、3. 男士西裝褲、4.女式西裝群、5.女士襯衫), 輸入0代表不選擇:')
if ad_type == '1':
ad_price11 = input('請問單價:')
price11 = ad_price11
adjust = input('有要修改單價(y/n):')
elif ad_type == '2':
ad_price21 = input('請問單價:')
price21 = ad_price21
adjust = input('有要修改單價(y/n):')
elif ad_type == '3':
ad_price31 = input('請問單價:')
price31 = ad_price31
adjust = input('有要修改單價(y/n):')
elif ad_type == '4':
ad_price41 = input('請問單價:')
price41 = ad_price41
adjust = input('有要修改單價(y/n):')
elif ad_type == '5':
ad_price51 = input('請問單價:')
price51 = ad_price51
adjust = input('有要修改單價(y/n):')
else:
break
clotype = input('請問採購哪一種衣服(1. 男士襯衫、2. 男士運動衫、3. 男士西裝褲、4.女式西裝裙、5.女士襯衫), 輸入0代表不選擇:')
while clotype != 0:
if clotype == '1':
number = int(input('請問購買幾件:'))
total11 = price11*number
shirt = shirt()
print('購買', number,'件男士襯衫')
shirt.set_mp1('男士襯衫', total11)
answer = input('是否繼續採購?(y/n)')
elif clotype == '2':
number = int(input('請問購買幾件:'))
total21 = price21*number
Tshirt = Tshirt()
print('購買', number,'件男士運動衫')
Tshirt.set_mp1('男士運動衫', total21)
answer = input('是否繼續採購?(y/n)')
elif clotype == '3':
number = int(input('請問購買幾件:'))
total31 = price31*number
pants = pants()
print('購買', number,'件男士西裝褲')
pants.set_mp1('男士西裝褲', total31)
answer = input('是否繼續採購?(y/n)')
elif clotype == '4':
number = int(input('請問購買幾件:'))
total41 = price41*number
suitskirt = suitskirt()
print('購買', number,'件女式西裝裙')
suitskirt.set_mp1('女式西裝裙', total41)
answer = input('是否繼續採購?(y/n)')
elif clotype == '5':
number = int(input('請問購買幾件:'))
total51 = price51*number
blouse = blouse()
print('購買', number,'件女士襯衫')
blouse.set_mp1('女士襯衫', total51)
answer = input('是否繼續採購?(y/n)')
while answer=="y":
clonewtype = input('請問採購哪一種衣服(1. 男士襯衫、2. 男士運動衫、3. 男士西裝褲、4.女式西裝裙、5.女士襯衫), 輸入0代表不選擇:')
if clonewtype == '1':
number = int(input('請問購買幾件:'))
total11 = price11*number
print('購買', number,'件男士襯衫')
shirt.set_mp1('男士襯衫', total11)
answer = input('是否繼續採購?(y/n)')
elif clonewtype == '2':
number = int(input('請問購買幾件:'))
total21 = price21*number
print('購買', number,'件男士運動衫')
Tshirt.set_mp1('男士運動衫', total21)
answer = input('是否繼續採購?(y/n)')
elif clonewtype == '3':
number = int(input('請問購買幾件:'))
total31 = price31*number
print('購買', number,'件男士西裝褲')
pants.set_mp1('男士西裝褲', total31)
answer = input('是否繼續採購?(y/n)')
elif clonewtype == '4':
number = int(input('請問購買幾件:'))
total41 = price41*number
print('購買', number,'件女式西裝裙')
suitskirt.set_mp1('女式西裝裙', total41)
answer = input('是否繼續採購?(y/n)')
elif clonewtype == '5':
number = int(input('請問購買幾件:'))
total51 = price51*number
print('購買', number,'件女士襯衫')
blouse.set_mp1('女士襯衫', total51)
answer = input('是否繼續採購?(y/n)')
else:
break
print('男士襯衫共計', total11, '元','男士運動衫共計', total21, '元','男士西裝褲共計', total31, '元','女士西裝裙共計', total41, '元','女士襯衫共計', total51, '元')
if (total11+total21+total31+total41+total51) > 500:
ftotal =int( (total11+total21+total31+total41+total51) * 0.7)
print('打7折之後應付', ftotal, '元')
elif (total11+total21+total31+total41+total51) > 300:
ftotal = int((total11+total21+total31+total41+total51) * 0.9)
print('打9折之後應付', ftotal, '元')
else:
print('因不足額無折扣優惠共計', (total11+total21+total31+total41+total51),'元' )
不,問題是在他沒寫的程式碼...
意思是我沒有在任何類別內寫到的意思嗎?
不好意思剛踏入真的很菜