以下代码返回人员的BMI风险 - 低,中或高.它工作得很好.但是,我想知道是否有其他方法可以解决它而不使用太多的返回语句.
有没有其他方法,Pythonic或逻辑上让它更短?
def bmi_risk(bmi, age):
''' function returning bmi's risk on human '''
if bmi < 22 and age < 45:
return "Low"
if bmi < 22 and age >= 45:
return "Medium"
if bmi >= 22 and age < 45:
return "Medium"
if bmi >= 22 and age >= 45:
return "High"
Run Code Online (Sandbox Code Playgroud) 我想要一个计算数组中字母出现次数的代码.我已经查看了各种准确的代码,但它们都使用字符串.我的问题是严格使用数组.我创建了一个代码,但它返回:: 0 : 1 : 2 : 3 : 4 : 5 : 6 : 7 : ...
一个正确的例子: 输入:
The quick brown fox jumps over the lazy dog.
Run Code Online (Sandbox Code Playgroud)
输出:
A: 1
B: 1
C: 1
D: 1
E: 3
F: 1 ...
Run Code Online (Sandbox Code Playgroud)
以下是我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main(void)
{
int i = 0;
int c;
char counts[26] = {0};
c = getchar();
while (c != EOF && i < 26) { …
Run Code Online (Sandbox Code Playgroud) 我需要编写一个函数,该函数接受一个int列表,nums
如果序列1, 2, 3, ..
出现在列表中的某个地方,则返回True 。
我的方法:
def list123(nums):
num = ""
for i in nums:
num += i
if "1,2,3" in num:
return True
else:
return False
Run Code Online (Sandbox Code Playgroud)
它无法正常工作,表明: builtins.TypeError: Can't convert 'int' object to str implicitly
我还想知道是否有更简单的方法,而不是像我一样将列表转换为字符串。