Python手机类---打印无

时间:2011-12-25 05:12:27

标签: python python-3.x

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文件。当我运行它时,每次打印后都会打印出无。我不明白为什么。我知道当你在python中的函数没有返回时它将返回None,但我不明白这里发生了什么。任何其他反馈也很棒。现在我有一个对象Phone,有一个菜单和其他东西。告诉我你是否还有另一种方法可以解决这个问题。

2 个答案:

答案 0 :(得分:6)

这是因为:

input(print("(1) See your phone specs \n(2) Change information\n Enter a Number: "))

print()函数不返回由input()打印的任何内容(即None),您不需要调用print()函数,因此它应该如下所示:

input("(1) See your phone specs \n(2) Change information\n Enter a Number: ")

答案 1 :(得分:1)

您看到input(print("..."))的任何地方,将其更改为input("...")。我的猜测是它被print()功能所吸引,并且很乐意打印无。

请务必将其标记为python3.x,因为这绝对不是2.x问题。