如何在Python中定义函数? [英] How to define functions in Python?

查看:118
本文介绍了如何在Python中定义函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前在python中编写一个程序来确定保龄球的费率,检查是否适用特价,并返回每人的费用。我必须使用赋值和if / else语句定义三个函数。



我编写了代码,但是我没有完成所有测试。有人可以查看并告诉我是否存在逻辑错误?我已经使用了python可视化工具,但它表明它应该工作;它没有。



我尝试过:



 def get_rate(start_hr,start_min,day,is_holiday):
if(is_holiday == True):
if(start_hr< = 18)和(start_min> = 29):
rate = 4
return 4
else:
rate = 6
return 6
else:
if(day< = 4):
if(start_hr< = 18)和(start_min< 30):
rate = 3
return 3
else:
rate = 5
返回5
否则:
if(day == 5):
if(start_hr< = 18)和(start_min> = 29):
rate = 3
返回3
否则:
rate = 6
返回6
否则:
if(day> = 6):
if(start_hr< ; = 18)和(start_min> = 29):
rate = 4
return 4
else:
rate = 6
return 6




def check_nite(start_hr,start_min,day):
if(day< = 5)和(start_hr> = 21)和(start_min> = 30):
ret urn True
else:
return False



def get_fee(rate,num_games,num_people,day,is_nite,is_holiday):
cost =(rate * num_games)* num_people
if is_holiday == True:
return('节日快乐!你的总金额是'',费用,'。')
其他:如果is_nite = =真
:如果是白天,则为
< = 4
rate = 7
num_games = 1
返回('nite special!你的总数是$',费用,'。')
else:
rate = 14
num_games = 1
返回14
else:
if(day == 6)and(num_people> = 4):
rate = 2
return('节俭的星期日!你的总额是$',成本, '。')
else:
return('你的总数是$',成本,'。')

解决方案

',cost,'。')
else:
if is_nite == True:
if day< = 4
rate = 7
num_games = 1
返回('nite special!你的总数是


',费用,'。')
else:
rate = 14
num_games = 1
返回14
else:
if(day == 6)and(num_people> = 4):
rate = 2
return('节俭的星期日!你的总数是


',cost,'。 )
,否则:
返回(你总为

I am currently writing a program in python to determine rates for bowling, check if specials are applicable, and returning the cost per person. I must define three functions using assignment and if/else statements.

I wrote the code, however it is failing all tests that I put it through. Can someone look it over and tell me if there is a logical error? I have used python visualizer and it shows that it should work, however; it does not.

What I have tried:

def get_rate(start_hr, start_min, day, is_holiday):
	if (is_holiday ==True):
		if (start_hr <=18) and (start_min >=29):
			rate=4
			return 4
		else:
			rate=6
			return 6
	else:
		if (day <=4): 
			if (start_hr <=18) and (start_min <30):
				rate= 3
				return 3
			else:
				rate=5
				return 5
		else: 
			if (day == 5):
				if (start_hr <=18) and (start_min >=29):
					rate=3
					return 3
				else:
					rate=6
					return 6
			else:		
				if (day >=6):
					if (start_hr <= 18) and (start_min >=29):
						rate=4
						return 4
					else:
						rate=6
						return 6




def check_nite(start_hr, start_min, day):
	if (day <=5) and (start_hr>=21) and (start_min >=30):
		return True
	else:
		return False



def get_fee(rate, num_games, num_people, day, is_nite, is_holiday):
	cost=(rate*num_games)*num_people
	if is_holiday== True: 
		return ('Happy holidays! your total is $', cost, '.')
	else: 
		if is_nite== True:
			if day <=4
				rate=7
				num_games=1
				return ('nite special! your total is $', cost,'.')
			else:
				rate=14
				num_games=1
				return 14
		else:
			if (day == 6) and (num_people>=4):
				rate=2
				return ('thrifty sunday! your total is $', cost, '.')
			else:
				return ('your total is $', cost,'.')

解决方案

', cost, '.') else: if is_nite== True: if day <=4 rate=7 num_games=1 return ('nite special! your total is


', cost,'.') else: rate=14 num_games=1 return 14 else: if (day == 6) and (num_people>=4): rate=2 return ('thrifty sunday! your total is


', cost, '.') else: return ('your total is


这篇关于如何在Python中定义函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆