如何压缩这个简单的python程序以提高效率?

时间:2017-10-12 13:10:43

标签: python python-3.x optimization simplify

这只是一个我试图整理的简单游戏,会问你几个问题,然后自动格式化答案中的新密码。

由于我是初学者,我想知道一些我可以使用的技术,这些技术可以提高代码的效率,并且可能会删除我声明的疯狂数量的函数。谢谢!

minimum = 3
maximum = 10

name = input("What is your name? ")

def nameInput():

    if len(name) < minimum:
        print("Name is too small")
        return nameInput()
    elif len(name) > maximum:
            print("Name is too large")
            return nameInput()

nameInput()

food = input("What is your favorite food? ")

def foodInput():

    if len(food) < minimum:
        print("Enter a food name longer than " + (food))
        return foodInput()
    elif len(food) > maximum:
            print("Enter a food name shorter than " + (food))
            return foodInput()

foodInput()

birthday = input("What is your date of birth? (mmddyyyy) ")

nameIndex = name[0:2]
foodIndex = food[2: ]
birthIndex = birthday[0:3]

passcode = nameIndex + foodIndex + birthIndex

print("Your password is " + passcode)

1 个答案:

答案 0 :(得分:1)

如果您在简洁之后,尝试多次使用单个函数而不是一次使用多个函数

float textViewResultSC = Float.parseFloat(mgetScore.getStringExtra("SkillsScore"));