栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python 小目标2

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python 小目标2

Python Day 8

Problem 1: Factorial

Write a factorial function that takes a positive integer,  N as a parameter and prints the result of  N!

Solution:

def factor(n):
    a=1
    for i in range (1,n+1):
 a=a*i
    print(a)
factor(3)
6
factor(5)
120
Problem 2: Age Create a function age() should perform the following conditional actions:
  • If n is between 0 to 12, print Young.
  • If 13 and 19, print Teenager.
  • Otherwise, print adult.
  • If age is smaller than 0, that’s Not valid
def age(n):
    if n<=0 :
 print ("Not valid")
    elif n in range (0,14):
 print ("Young")
    elif n in range (13,19):
 print ("Teenager")
    else:
 print("adult")

age(-1)
Not valid

[caption id=“attachment_1642” align=“alignnone” width=“500”]

cbaquiran / Pixabay[/caption]

Problem 3: Do Loop

Given an integer, n, print its first 10 multiples. Each multiple n x i , should be printed on a new line in the form: n x i = result

def mul2(n):
    count=0
    while count <11:
 print(count, "x", n, "=", count*n)
 count +=1
mul2(3)
0 x 3 = 0
1 x 3 = 3
2 x 3 = 6
3 x 3 = 9
4 x 3 = 12
5 x 3 = 15
6 x 3 = 18
7 x 3 = 21
8 x 3 = 24
9 x 3 = 27
10 x 3 = 30
Problem 4: Odd-Even Position

Given a string, S, of length N  that is indexed from 0 to N-1 , print its even-indexed and *odd-indexed *characters as 2 space-separated strings on a single line

def sep_string(a):
    l=list(a)
    l2=list()
    l1=list()
    index=0

    for letter in l:
 if index %2 ==1:
    l1.append(letter)
 else:
    l2.append(letter)
 index +=1
    string_even = " ".join(l2)
    string_odd=" ".join(l1)
    print(string_even.replace(" ", ""), " ",  string_odd.replace(" ",""))

sep_string("Wonderful")
Wnefl   odru

Happy Studying! 酪

转载请注明:文章转载自 www.mshxw.com
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号