Python 电话类 --- 打印 None
class Phone:
def __init__(self):
self.types = ["Touch Screen","Flip", "Slider", "Bar", "Bag"]
self.brand = "No Brand Determined"
self.type_of_phone = "No Type of Phone has been selected"
def get_type(self):
return self.type_of_phone
def change_type(self, changeTo):
if self.check_type(changeTo):
self.type_of_phone = changeTo
else:
print("The Type you wish to change the phone to is not a supported type.")
def change_brand(self, changeTo):
self.brand = changeTo
def check_type(self, inQuestion):
if inQuestion in self.types:
return True
return False
def toString(self):
return "Brand: "+self.brand+"\nType: "+self.type_of_phone+"\n"
def menu(self):
self.intro()
while True:
self.mainScreen()
def intro(self):
print("This program will let you create a cell phone type and brand.")
def mainScreen(self):
option = input(print("(1) See your phone specs \n(2) Change information\n Enter a Number: "))
if option == "1":
print("\n"+self.toString())
elif option == "2":
self.changeScreen()
else:
print("Enter 1 or 2. Please.")
def changeScreen(self):
option = input(print("\nWould you like to change the...\n(1) Type\n(2) Brand\n Enter a Number: "))
if option == "1":
self.changeMyType()
elif option == "2":
self.changeMyBrand()
else:
print("Enter 1 or 2")
self.changeScreen()
def changeMyType(self):
optionType = input(print("\nThese are your options of phones: \n",self.types,"\nEnter an option [case sensitive]: "))
self.change_type(optionType)
def changeMyBrand(self):
optionBrand = input(print("\nEnter the Brand you would like your phone to be: "))
self.change_brand(optionBrand)
def main():
#commands created that fully work:
#Types of Phones to change to: Touch Screen, Flip, Slider, Bar, Bag
#get_type()
#change_type()
myPhone = Phone()
myPhone.menu()
main()
运行这个Python文件。当我运行它时,每次打印后都会显示None。我不明白为什么。我知道在Python中,如果你的函数没有返回值,它会返回None,但我不明白这里发生了什么。任何其他的反馈也很好。目前我有一个Phone对象,它有一个菜单和其他东西。如果你有其他的方法来处理这个问题,请告诉我。
2 个回答
6
这是因为:
input(print("(1) See your phone specs \n(2) Change information\n Enter a Number: "))
print()这个函数其实不返回任何东西(也就是返回None),所以input()并不会打印出什么。你不需要调用print()这个函数,因此应该这样写:
input("(1) See your phone specs \n(2) Change information\n Enter a Number: ")
1
你看到的每一个 input(print("..."))
,都要改成 input("...")
。我猜这是因为它被 print()
函数搞混了,结果会很乐意地打印出 None。
记得把这个标记为 python3.x,因为这绝对不是 2.x 的问题。