我是python的初学者
如果输入为5,如何使输出33仅打印一次
最重要的问题是:是否有一种算法可以取代写21的需要
原来的问题是:
It is commonly said that one human year is equivalent to 7 dog years.Some people believe that it is better to count each of the first two human years as 10.5 dog years, and then count each additional human year as 4 dog years
dog_years=int(input("Enter the age of the dog to get it's equivalent for it's owner:" ))
dog_age_to_human_age_1=10.5*dog_years
dog_age_to_human_age_2=4*(dog_years-2)
dog_1=[]
if dog_years <= 0:
print("Please Enter an positive integer whole number")
exit()
elif dog_years > 0 and dog_years <= 2:
print(dog_age_to_human_age_1)
dog_1.append(dog_age_to_human_age_1)
for dog_years in range(3,dog_years+1):
dog_1.append(dog_age_to_human_age_2)
print(dog_1[-1]+21) #is there an algorithm to replace the 21?
不同转换技术的性能
检查了以下方法
结果
测试1:不同长度的年龄列表,随机年龄在0到25岁之间
测试2:年龄从0岁到2**10岁不等(尽管年龄超过25岁对狗来说是不现实的)
代码
测试1:从0到25岁的随机年龄,改变年龄数量
输出
测试2:年龄从0岁到1024岁不等
输出
我重新编写了您的代码,以获得所需的功能:
可以使用NumPy替换for循环,如下所示:
这个问题可以更优雅地用算术来解决:
相关问题 更多 >
编程相关推荐