sdsd 发表于 2017-8-12 11:01:16

def task(w,n,c):
      week_nam={'Monday':'James','Tuesday':'John','Wednesday':'Robert',
                        'Thursday':'Michael','Friday':'William'}
      sum_cost=n*c
      return 'It is '+w+','+week_nam+',you have to work,you must spray '+str(n)+' trees and you need '+str(sum_cost)+' dollars to buy liquid'

龙潭小天才 发表于 2017-8-12 16:26:08

def task(w,n,c):
    task_order={'monday':'james','Tuesday':'john','Wednesday':'robert','Thursday':'michael','friday':'william'}
    return('it is {}today,{},you have to work,you must spray{}trees and you need{}dollars to buy liquid'.format(w,task_order,n,n*c))
   

chunchun2017 发表于 2017-8-12 17:11:30

本帖最后由 chunchun2017 于 2017-8-12 21:40 编辑

def task(w,n,c):
    if(w=='Monday'):
      person='James'
    elif(w=='Tuesday'):
      person='John'
    elif(w=='Wednesday'):
      person='Robert'
    elif(w=='Thursday'):
      person='Michael'
    elif(w=='Friday'):
      person='William'
    print('It is %s today, %s, you have to work, you must spray %d trees and you need %d dollars to buy liquid' % (w,person,n,c*n))

或者用字典更简洁
def task(w,n,c):
    dict0={'Monday':'James','Tuesday':'John','Wednesday':'Robert','Thursday':'Michael','Friday':'William'}
    print('It is %s today, %s, you have to work, you must spray %d trees and you need %d dollars to buy liquid' % (w,dict0,n,c*n))

1173419450 发表于 2017-8-13 20:25:38

看看

solomonxian 发表于 2017-8-19 18:40:21

就是字符串操作
def task(w,n,c):
    dic = {'Monday':"James","Tuesday":"John","Wednesday":"Robert","Thursday":"Michael","Friday":"William"}
    try:
      return 'It is %s today, %s, you have to work, you must spray %d trees and you need %d dollars to buy liquid'%(w,dic,n,c*n)
    except KeyError:
      return "No one works on %s"%w

colinshi 发表于 2017-10-19 16:09:00

古堡主人。 发表于 2017-8-11 21:18
没有抄袭,只是发了两次,两次都是我发的,不过你们竟然能这么快恢复也是让我眼前一亮,不和你聊了,我去 ...

其实我想告诉你爬草榴的精髓不在于python,而在于如何翻墙。

克里斯保罗 发表于 2019-9-24 07:35:42

def task(w,n,c):
          stuff=['James','John','Robert','Michael','William']
          day =['Monday','Tuestday','Wednesday','Thursday','Friday']
          day_cn = ['星期一','星期二','星期三','星期四','星期五']
          index = day_cn.index(w)
          dollars = n*c
          result = "It is {} today,{},you have to work,you must spray {}trees and you need {} dollars to buy liquid".format(day,stuff,n,dollars)
          print(result)
task('星期一',15,2)

学会做蛋饼 发表于 2019-9-24 12:20:44

dict1 = {"Monday":"James" ,"Tuesday":"John","Wednesday":"Robert","Thursday":"Michael","Friday":"William"}
w = input("请输入日期:(除周末外)")
n = input("请输入当天必须喷洒的树木数量:")
c = input("请输入1升液体的成本")
def task(w,n,c):
    n = int(n)
    c = int(c)
    mul = n * c
    print("It is %s today,%s,you have to work,yuo must spray %d and you need %d dollars to buy liquid" % (w,dict1,n,mul))
   
task(w,n,c)

holiday_python 发表于 2020-6-29 13:14:46

19971023 发表于 2020-7-30 10:48:19

1

aironeng 发表于 2020-12-9 09:17:41

学习
页: 1 [2]
查看完整版本: Python:每日一题 76(答题领鱼币)