PYTHON Fantasy Adventure 18 Make a small project

PYTHON Fantasy Adventure 18 Make a small project

Every day, it only takes up ten minutes for everyone to eat at noon. If you have anything to do, take a look at it and master a language easily! Let us work hard together.

Xiaopengpeng’s fantasy adventure came to Las Vegas, USA, and spent a lot of time in order to post questions about my brother! I wanted to go home early to rest! Unexpectedly, I met a letter from a mysterious woman! She knows the map to the next village. But because her family business is too large, she can't be busy alone. So she needs us to help her realize a task of project management! Xiaopengpeng didn't want to help, but she appeared!

This favor must be helped~ For the goddess, Xiaopengpeng is obliged to do it! Even if he goes up to the sword, he is willing to go down to the sea of ​​fire!
Goddess, remember to sign and take a photo with me~
If there is a new movie, remember to tell me~

The goddess is here!

TASK: Workload calculator

I don’t know what you usually do when you encounter a problem? One thing is very important! Just learn to check information! Because we can't remember too many knowledge points! You can only rely on yourself to keep taking notes and accumulate!

Requirement 1: What is the calculation time
? Do not be afraid! The project manager will tell you the needs! It's actually very simple, just the division formula!

1 project, one person needs 80 hours to complete, 10 hours work per day,
1.5 projects (120 hours) in 8 days ! => Two people! => It takes 60 hours

Output: Working time
requirement 2: Manpower calculation

0.5 projects (40 hours)! => Complete within 20 hours! => Need 2 people!
Output: number of people working

The above is our task!
When encountering such a problem, the first step is to briefly describe it!

# 注:size代表项目大小,number代表人数,time代表工时数量
choice = int(input("请输入工作计时器的功能:"))
# 1计算时间,0计算人力
if choice == 1 :
    size = 1.5
    number = 2
    time = size * 80 / number
    print('项目大小为%.1f个标准项目,使用%d个人力完成,则需要工时数量为%d:' % (size, number,time))
else:
    # 人力计算
    size = 0.5
    time = 20.0
    number = size * 80 /time
    print('项目大小为%.1f个标准项目,如果需要在%.1f个工时完成,则需要人力数量为%d:' % (size, time, number))

operation result:

请输入工作计时器的功能:0 
项目大小为0.5个标准项目,如果需要在20.0个工时完成,则需要人力数量为2

That's the function! But our current version can't do anything, we still need to upgrade constantly! Many details require constant consideration by everyone! It is best to try it yourself! Next is the official start! Now is the real time for a programmer to perform! No matter how good it is, it is useless! The depth of the foundation determines our upper limit. Just like KOBE, you never know how hard he has worked! To succeed, you have to pay sweat and tears!

Just use Version1

Not much to say, look at the code first, in the world of PYTHON, satisfy your fantasy together!
Use what you learned in the previous section! Create your own function!

choice = int(input("请输入工作计时器的功能:"))
def computer_time(choice,size, number):
    
    # 1计算时间,0计算人力
    if choice == 1 :
    #size = 1.5
    #number = 2
        time = size * 80 / number
        print('项目大小为%.1f个标准项目,使用%d个人力完成,则需要工时数量为%d:' % (size, number,time))
#else:
def computer_number(choice,size,time):
    # 人力计算
    if choice == 2:
    #size = 0.5
    #time = 20.0
    	number = size * 80 /time
    	print('项目大小为%.1f个标准项目,如果需要在%.1f个工时完成,则需要人力数量为%d:' % (size, time, number))

computer_time(choice,1.5,2)
computer_number(choice,0.5,20)

operation result

请输入工作计时器的功能:2
项目大小为0.5个标准项目,如果需要在20.0个工时完成,则需要人力数量为2

%f means that the formatted string is of floating-point type, and %.1f means that the formatted string is of floating-point type, with 1 decimal place.

Version2 version improvement

Not much to say, look at the code first, in the world of PYTHON, satisfy your fantasy together!
In fact, the above code still has some problems! Maybe we can add a decimal point in time, but what about people? 1.2 How many people are individuals? So now we have to solve this problem!

What needs attention is the ceil function! It is rounded up~


# 注:size代表项目大小,number代表人数,time代表工时数量
import math

choice = int(input("请输入工作的类型:"))
def computer_time(choice , size, number):
    global time
    # 工时计算
    if choice == 1 :
        # size = 1.5
        # number = 2
        time = math.ceil(size * 80 / number)
        print('项目大小为%.1f个标准项目,使用%d个人力完成,则需要工时数量为%d:' % (size, number,time))
def computer_number(choice , size ,time):
    global number 
    if choice == 2 :
        # 人力计算
        #size = 0.5
        #time = 20.0
        number = math.ceil(size * 80 /time)
        print('项目大小为%.1f个标准项目,如果需要在%.1f个工时完成,则需要人力数量为%d:' % (size, time, number))
    
computer_time(choice,1.5,2)
computer_number(choice,2.5,20)

operation result:

请输入工作的类型:1
项目大小为1.5个标准项目,使用2个人力完成,则需要工时数量为60

Is this all right? Optimized version! Below we hope that the code can be simpler!

import math

# 为函数设置了三个参数,并都带有默认参数
def computer(size=1,number=None,time=None):
    # 人力计算:如果参数中填了时间,没填人数,就计算人力
    if (number == None) and (time != None):
        number = math.ceil(size * 80 / time)
        print('项目大小为%.1f个标准项目,如果需要在%.1f个工时完成,则需要人力数量为:%d人' %(size,time,number))  
    # 工时计算:如果参数中填了人数,没填时间,就计算工时
    elif (number != None) and (time == None):
        time = size * 80 / number
        print('项目大小为%.1f个标准项目,使用%d个人力完成,则需要工时数量为:%.1f个' %(size,number,time))  

# 调用函数的时候,传递两个参数,会自动计算出第三个参数
computer(size=1.5,number=2)
computer(size=0.5,time=20.0)

Please take a closer look! Isn't this much better!
operation result:

项目大小为1.5个标准项目,使用2个人力完成,则需要工时数量为:60.0个
项目大小为0.5个标准项目,如果需要在20.0个工时完成,则需要人力数量为:2

Version3 success or failure in one fell swoop

After the above continuous efforts, is our project already OK? Of course not! The things that promise the goddess must be done to the extreme, even if it's a brother, it's not bad for money in the past few years!
Not much to say, look at the code first, in the world of PYTHON, satisfy your fantasy together!

# 请大家一定要认真看一下!先将下面的代码改造成两个子函数和一个主函数,并调用主函数。

import math

# 第一步先定义一下采集信息的函数
def myinput():
    choice = input('请选择计算类型:(1-工时计算,2-人力计算)')
    if choice == '1':
        size   = float(input('请输入项目大小:(1代表标准大小,请输入小数)'))
        number = int(input('请输入人力数量:(请输入整数)'))
        time = None
        return size,number,time
        # 这里返回的数据是一个元组
    if choice == '2':
        size = float(input('请输入项目大小:(1代表标准大小,请输入小数)'))
        number = None
        time = float(input('请输入工时数量:(请输入小数)'))
        return size,number,time
        # 这里返回的是一个元组

# 完成计算的函数
def estimated(my_input):
    # 把元组中的数据取出来
    size   = my_input[0]
    number = my_input[1]
    time   = my_input[2]
    # 人力计算
    if (number == None) and (time != None):
        number = math.ceil(size * 80 / time)
        print('项目大小为%.1f个标准项目,如果需要在%.1f个工时完成,则需要人力数量为:%d人' %(size,time,number)) 
    # 工时计算
    elif (number != None) and (time == None):
        time = size * 80 / number
        print('项目大小为%.1f个标准项目,使用%d个人力完成,则需要工时数量为:%.1f个' %(size,number,time))  

# 主函数
def main():
    my_input = myinput() # 开辟一个地址空间!
    estimated(my_input)  # 将my_input当成参数

# 调用主函数
main()

operation result

请选择计算类型:(1-工时计算,2-人力计算)1 
请输入项目大小:(1代表标准大小,请输入小数)3.1 
请输入人力数量:(请输入整数)5 
项目大小为3.1个标准项目,使用5个人力完成,则需要工时数量为:49.6

At this point, has the code become taller? But it's not over yet! Our mission is not over! The goddess is still not satisfied, so continue to upgrade! Think about the design of the game, should our code also learn from it! Everything must have a beginning and an end! This is the beginning and the end!
Not much to say, look at the code first, in the world of PYTHON, satisfy your fantasy together!

import math

# 变量key代表循环运行程序的开关,这就是我们新加的设计!
key = 1

# 采集信息的函数
def myinput():
    choice = input('请选择计算类型:(1-工时计算,2-人力计算)')
    if choice == '1':
        size = float(input('请输入项目大小:(1代表标准大小,请输入小数)'))
        number = int(input('请输入人力数量:(请输入整数)'))
        time = None
        return size,number,time
        # 这里返回的数据是一个元组
    if choice == '2':
        size = float(input('请输入项目大小:(1代表标准大小,请输入小数)'))
        number = None
        time = float(input('请输入工时数量:(请输入小数)'))
        return size,number,time
        # 这里返回的是一个元组

# 完成计算的函数
def estimated(my_input):
    # 把元组中的数据取出来
    size = my_input[0]
    number = my_input[1]
    time = my_input[2]
    # 人力计算
    if (number == None) and (time != None):
        number = math.ceil(size * 80 / time)
        print('项目大小为%.1f个标准项目,如果需要在%.1f个工时完成,则需要人力数量为:%d人' %(size,time,number)) 
    # 工时计算
    elif (number != None) and (time == None):
        time = size * 80 / number
        print('项目大小为%.1f个标准项目,使用%d个人力完成,则需要工时数量为:%.1f个' %(size,number,time))  

# 询问是否继续的函数,这就是设计的创新点
def again():
    # 声明全局变量key,以便修改该变量
    global key
    a = input('是否继续计算?继续请输入y,输入其他键将结束程序。')
    if a != 'y':
        # 如果用户不输入'y',则把key赋值为0
        key = 0  

# 主函数
def main():
    print('女神,欢迎使用工作量计算小程序!')
    while key == 1:
        my_input = myinput()
        estimated(my_input)
        again()
    print('感谢使用工作量计算小程序!')

main() # 开辟地址空间

I wonder if you have noticed a detail! For example, def main, def computer, def my_input, and def main contains functions such as computer and my_input. Why is there still main() at the end? Is it necessary to open up the address space? This question is left for everyone to think about! Solve the problem with the problem!
There are detailed answers in this article below!

https://www.zhihu.com/question/49136398 

In fact, main() is the entry point of a program!

operation result


欢迎使用工作量计算小程序!
请选择计算类型:(1-工时计算,2-人力计算)2
请输入项目大小:(1代表标准大小,请输入小数)9.0
请输入工时数量:(请输入小数)8
项目大小为9.0个标准项目,如果需要在8.0个工时完成,则需要人力数量为:90人
是否继续计算?继续请输入y,输入其他键将结束程序。n
感谢使用工作量计算小程序!

Boss is coming

The content of this section is actually quite complicated! The reason for adding some animated pictures is to make everyone not feel boring and boring! Language learning still depends on your own passion! If you regard it as a good friend, all problems will be very simple! Knowledge is not about learning too much, but about helping us learn a kind of thinking! One way to solve the problem! For the PYTHON series, I will always insist on originality and no charge! Good things must be shared with everyone! At the same time, originality is not easy, and it needs everyone's support and encouragement. thank you all! BOSS is going to be upgraded this time! Leave a suspense for everyone! See you in the next issue!

Solemnly explain: We only talk about the learning method of PYTHON, not how to pursue the goddess. If there is a good way, I can write an article to tell everyone!
The next issue will be more exciting! We do not say goodbye!

Welcome everyone to read my "The Weakness of Human Nature" to give you a different feeling!

Guess you like

Origin blog.csdn.net/weixin_46259642/article/details/112434736