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'
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 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)) 看看 就是字符串操作
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 古堡主人。 发表于 2017-8-11 21:18
没有抄袭,只是发了两次,两次都是我发的,不过你们竟然能这么快恢复也是让我眼前一亮,不和你聊了,我去 ...
其实我想告诉你爬草榴的精髓不在于python,而在于如何翻墙。 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)
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)
是 1 学习
页:
1
[2]