其他分享
首页 > 其他分享> > 实验4

实验4

作者:互联网

 

 

print(sum)
sum = 42
print(sum)
def inc(n):
   sum = n+1
   print(sum)
   return sum
sum = inc(7) + inc(7)
print(sum)

 1.内置函数

2.给sum赋予一个值

3.局部变量

3.全局变量

2.

list1 = [1, 9, 8, 4]
print( sorted(list1) )
print( sorted(list1, reverse=True) )
print( sorted(list1, True) )

 

 不需要

def func(a, b, c, /, *, d, e, f):
   return( [a,b,c,d,e,f] )
print(func(1,2,3,d=4,e=5,f=6))

 

 

3.

def solve(a, b, c):
   delta = b*b - 4*a*c
   delta_sqrt = abs(delta)**0.5
   p1 = -b/2/a;
   p2 = delta_sqrt/2/a
   if delta>=0:
      root1 = p1 + p2
      root2 = p1 - p2
   else:
      root1 = complex(p1, p2)
      root2 = complex(p1, -p2)
   return root1, root2
print(solve.__doc__)
while True:
   try:
      a,b,c = eval(input('Enter eqution coefficient: '))
      if a == 0:
         raise
   except:
      print('invalid input, or, a is zero')
      break
   else:
      root1, root2 = solve(a, b, c)
      print(f'root1 = {root1:.2f}, root2 = {root2:.2f}')
      print()

 

 4.

def list_generator(a,b,c):
    list=[]
    if type(c)==int:
       for i in range(a,b+1,c):
          list.append(i)
       return list
    elif type(c)!=int:
        g=1
        e=int(a/c)
        f=int(b/c)
        for i in range(e,f+1,g):
            list.append(c*i)
        return list

list1 = list_generator(-5, 5,1)
print(list1)
list2 = list_generator(-5, 5, 2)
print(list2)
list3 = list_generator(1, 5, 0.5)
print(list3)

 

 5.

def is_prime(n):
   list=[]
   for i in range(1,n):
         s=n%i
         list.append(s)
         y=list.count(0)
   if y>1:
         return False
   else:
         return True
def found(n):
    for i in range(2,n):
        if is_prime(i):
            if is_prime(n-i):
                print(n,'=',i,'+',n-i)
                return True
    return False
n=input()
i=4
while(found(i) and i<20):
    i+=2

 

 6.

 

def encorder(text):
    sentence = []
    for i in text:
        if i.isupper():
            sentence.append(chr((ord(i)-60)%26+65))
        elif i.islower():
            sentence.append(chr((ord(i)-92)%26+97))
        else:
            sentence.append(i)
    return ''.join(sentence)
def decorder(text):
    sentence = []
    for i in text:
        if i.isupper():
            sentence.append(chr((ord(i)-44)%26+65))
        elif i.islower():
            sentence.append(chr((ord(i)-76)%26+97))
        else:
            sentence.append(i)
    return ''.join(sentence)
text=input('输入英文文本;')
print(f'编码后的文本;{encorder(text)}')
print(f'解码后的文本;{decorder(encorder(text))}')

 

 

 

 7.

def collatz(n):
    list=[n]
    while list[-1]!=1:
        if list[-1]%2==0:
            list.append(list[-1]/2)
        else:
            list.append(3*list[-1]+1)
    return list
while True:
   try:
       n=eval(input('输入一个数字:'))
       if n<=0 or int(n)!=n:
           raise
   except:
       print('Error: must be a positive integer')
   else:
       print(collatz(n))

 

标签:return,sentence,sum,list,实验,print,append
来源: https://www.cnblogs.com/forced51767/p/16221619.html