Question 4
lst = input().split(',') # the input is being taken as string and as it is string it has a built in
# method name split. ',' inside split function does split where it finds any ','
# and save the input as list in lst variable
tpl = tuple(lst) # tuple method converts list to tuple
print(lst)
print(tpl)
Question 5
class IOstring():
def get_string(self):
self.s = input()
def print_string(self):
print(self.s.upper())
xx = IOstring()
xx.get_string()
xx.print_string()
Question 6
解法1:
from math import sqrt
C,H = 50,30
def calc(D):
return sqrt((2*C*D)/H)
D = input().split(',') # splits in comma position and set up in list
D = [str(round(calc(int(i)))) for i in D] # using comprehension method. It works in order of the previous code
print(",".join(D))
解法2:
from math import sqrt
C, H = 50, 30
mylist = input().split(',')
print(*(round(sqrt(2*C*int(D)/H)) for D in mylist), sep=",")
解法3:
my_list = [int(x) for x in input('').split(',')]
C, H, x = 50, 30, []
for D in my_list:
Q = ((2*C*D)/H)**(1/2)
x.append(round(Q))
print(','.join(map(str, x)))
Question 7
方法1:
x,y = map(int,input().split(','))
lst = []
for i in range(x):
tmp = []
for j in range(y):
tmp.append(i*j)
lst.append(tmp)
print(lst)
方法2:
x,y = map(int,input().split(','))
lst = [[i*j for j in range(y)] for i in range(x)]
print(lst)
Question 8
lst = input().split(',')
lst.sort()
print(",".join(lst))
Question 9
解法1:
lst = []
while True:
x = input()
if len(x)==0:
break
lst.append(x.upper())
for line in lst:
print(line)
解法2:
def inputs():
while True:
string = input()
if not string:
return
yield string
print(*(line.upper() for line in inputs()),sep='\n')