大家好,这是我正在为学校工作编写的示例代码。
class Item:
list = []
def __init__ (self, name, num):
Item.list.append(self)
self.name = name
self.num = num
bread = Item("Bread", 20)
rice = Item("Rice", 30)
fruits = Item("Fruits", 40)
vegetables = Item("Vegetables", 25)
meat = Item("Meat", 35)
for i in Item.list:
print(f"{i.name}, {i.num}")
def withdraw():
b = int(input(print("\nHow many bread would you take? ")))
r = int(input(print("\nHow many rice would you take? ")))
f = int(input(print("\nHow many fruits would you take? ")))
v = int(input(print("\nHow many vegetables would you take? ")))
m = int(input(print("\nHow many meat would you take? ")))
bread.num = bread.num - b
rice.num = rice.num - r
fruits.num = fruits.num - f
vegetables.num = vegetables.num - v
meat.num = meat.num - m
withdraw()
for i in Item.list:
print(f"{i.name} : {i.num}")
每次打印的输出都是这样
How many bread would you take
None
为什么打印 None?我怎么能阻止它。谢谢。
答案 0 :(得分:1)
尝试改变这些:
b = int(input(print("\nHow many bread would you take? ")))
r = int(input(print("\nHow many rice would you take? ")))
f = int(input(print("\nHow many fruits would you take? ")))
v = int(input(print("\nHow many vegetables would you take? ")))
m = int(input(print("\nHow many meat would you take? ")))
到:
b = int(input("\nHow many bread would you take? "))
r = int(input("\nHow many rice would you take? "))
f = int(input("\nHow many fruits would you take? "))
v = int(input("\nHow many vegetables would you take? "))
m = int(input("\nHow many meat would you take? "))