migration from cloud service
This commit is contained in:
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
*/__pycache__
|
||||||
|
__pycache__
|
||||||
|
*.pyc
|
||||||
26
week3/ex1.py
Normal file
26
week3/ex1.py
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
def main():
|
||||||
|
print('==Exercise 1==')
|
||||||
|
flag=True
|
||||||
|
while flag:
|
||||||
|
a = input("pls input number A:")
|
||||||
|
try:
|
||||||
|
a = int(a)
|
||||||
|
flag=False
|
||||||
|
except :
|
||||||
|
print("pls input a number!")
|
||||||
|
flag=True
|
||||||
|
while flag:
|
||||||
|
b = input("pls input number B:")
|
||||||
|
try:
|
||||||
|
b = int(b)
|
||||||
|
flag=False
|
||||||
|
except :
|
||||||
|
print("pls input a number!")
|
||||||
|
|
||||||
|
print(f"The result for A+B={a+b}")
|
||||||
|
|
||||||
|
print('==End Of Ex1==')
|
||||||
|
return 0
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
||||||
12
week3/ex2.py
Normal file
12
week3/ex2.py
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
def main():
|
||||||
|
list = ['My', 'Name', 'Is', 'James']
|
||||||
|
|
||||||
|
print('==Exercise 2==')
|
||||||
|
print(f'Raw print: {list}')
|
||||||
|
print(f'Spaced print: {list}',sep=" ")
|
||||||
|
print(f'stared print: {list}',sep="**")
|
||||||
|
print('==End Of Ex2==')
|
||||||
|
return 0
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
||||||
39
week3/ex3.py
Normal file
39
week3/ex3.py
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
def main():
|
||||||
|
usingJSON = False
|
||||||
|
al = []
|
||||||
|
print('==Exercise 3==')
|
||||||
|
if not usingJSON:
|
||||||
|
print('''I have a method to input all 5 floats in one time using JSON.
|
||||||
|
Pls set usingJSON = True in source code to try it.''')
|
||||||
|
for i in range(5):
|
||||||
|
flag=True
|
||||||
|
while flag:
|
||||||
|
a = input(f"pls input float {i+1}:")
|
||||||
|
try:
|
||||||
|
a = float(a)
|
||||||
|
flag=False
|
||||||
|
al.append(a)
|
||||||
|
except :
|
||||||
|
print("pls input a float!")
|
||||||
|
print(al)
|
||||||
|
else:
|
||||||
|
import json
|
||||||
|
print("In this case you need to enter a JSON string of the whole list.")
|
||||||
|
flag=True
|
||||||
|
while flag:
|
||||||
|
a = input("pls input float[] JSON:")
|
||||||
|
try:
|
||||||
|
a = json.loads(a)
|
||||||
|
states = [True if type(i)==float else False for i in a]
|
||||||
|
if False in states:
|
||||||
|
print(f"Error! not a float[]!\nCurrent Input:{a}")
|
||||||
|
raise ValueError("Not a float[]")
|
||||||
|
flag=False
|
||||||
|
al.append(a)
|
||||||
|
except :
|
||||||
|
print("pls input a proper float[]!")
|
||||||
|
print('==End Of Ex3==')
|
||||||
|
return 0
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
||||||
12
week3/ex4.py
Normal file
12
week3/ex4.py
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
def main():
|
||||||
|
totalMoney = 1000
|
||||||
|
quantity = 3
|
||||||
|
price = 450
|
||||||
|
|
||||||
|
print('==Exercise 4==')
|
||||||
|
print('I have {0} dollars so I can buy {1} football for {2:.2f} dollars.'.format(totalMoney,quantity,price))
|
||||||
|
print('==End Of Ex4==')
|
||||||
|
return 0
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
||||||
14
week3/ex5.py
Normal file
14
week3/ex5.py
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
def main():
|
||||||
|
list1 = ["M", "na", "i", "Ke"]
|
||||||
|
list2 = ["y", "me", "s", "lly"]
|
||||||
|
|
||||||
|
list3 = [f'{list1[i]}{list2[i]}' for i in range(len(list1))]
|
||||||
|
|
||||||
|
|
||||||
|
print('==Exercise 5==')
|
||||||
|
print(list3)
|
||||||
|
print('==End Of Ex5==')
|
||||||
|
return 0
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
||||||
15
week3/ex6.py
Normal file
15
week3/ex6.py
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
def main():
|
||||||
|
list1 = ["Hello ", "take "]
|
||||||
|
list2 = ["Dear", "Sir"]
|
||||||
|
|
||||||
|
mtx = [[0,0],[0,1],[1,0],[1,1]]
|
||||||
|
|
||||||
|
list3 = [f'{list1[mtx[i][0]]}{list2[mtx[i][1]]}' for i in range(len(mtx))]
|
||||||
|
|
||||||
|
print('==Exercise 6==')
|
||||||
|
print(list3)
|
||||||
|
print('==End Of Ex6==')
|
||||||
|
return 0
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
||||||
7
week3/run_all.py
Normal file
7
week3/run_all.py
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
import ex1,ex2,ex3,ex4,ex5,ex6
|
||||||
|
ex1.main()
|
||||||
|
ex2.main()
|
||||||
|
ex3.main()
|
||||||
|
ex4.main()
|
||||||
|
ex5.main()
|
||||||
|
ex6.main()
|
||||||
48
week4/ex1.py
Normal file
48
week4/ex1.py
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
print('==Exercise 1==')
|
||||||
|
SEPREATE_VAR_WORD_ALL="all"
|
||||||
|
SEPREATE_VAR_WORD_WORK="work"
|
||||||
|
SEPREATE_VAR_WORD_AND="and"
|
||||||
|
SEPREATE_VAR_WORD_NO="no"
|
||||||
|
SEPREATE_VAR_WORD_PLAY="play"
|
||||||
|
SEPREATE_VAR_WORD_MAKE="make"
|
||||||
|
SEPREATE_VAR_WORD_JACK="Jack"
|
||||||
|
SEPREATE_VAR_WORD_A="a"
|
||||||
|
SEPREATE_VAR_WORD_DULL="dull"
|
||||||
|
SEPREATE_VAR_WORD_BOY="boy"
|
||||||
|
SEPREATE_VAR_WORD_DOT="."
|
||||||
|
|
||||||
|
print(SEPREATE_VAR_WORD_ALL.capitalize(),
|
||||||
|
SEPREATE_VAR_WORD_WORK,
|
||||||
|
SEPREATE_VAR_WORD_AND,
|
||||||
|
SEPREATE_VAR_WORD_NO,
|
||||||
|
SEPREATE_VAR_WORD_PLAY,
|
||||||
|
SEPREATE_VAR_WORD_MAKE,
|
||||||
|
SEPREATE_VAR_WORD_JACK,
|
||||||
|
SEPREATE_VAR_WORD_A,
|
||||||
|
SEPREATE_VAR_WORD_DULL,
|
||||||
|
SEPREATE_VAR_WORD_BOY,
|
||||||
|
end=SEPREATE_VAR_WORD_DOT)
|
||||||
|
|
||||||
|
print('pls ignore the following line if youre using windows\' default cmd prompt e.g. cmd.exe. using bash to make it display properly.')
|
||||||
|
|
||||||
|
print('\033[3m\t',
|
||||||
|
SEPREATE_VAR_WORD_ALL.capitalize(),
|
||||||
|
SEPREATE_VAR_WORD_WORK,
|
||||||
|
SEPREATE_VAR_WORD_AND,
|
||||||
|
SEPREATE_VAR_WORD_NO,
|
||||||
|
SEPREATE_VAR_WORD_PLAY,
|
||||||
|
SEPREATE_VAR_WORD_MAKE,
|
||||||
|
SEPREATE_VAR_WORD_JACK,
|
||||||
|
SEPREATE_VAR_WORD_A,
|
||||||
|
SEPREATE_VAR_WORD_DULL,
|
||||||
|
SEPREATE_VAR_WORD_BOY,
|
||||||
|
end=SEPREATE_VAR_WORD_DOT)
|
||||||
|
|
||||||
|
print('==End Of Ex1==')
|
||||||
|
return 0
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
||||||
27
week4/ex2.py
Normal file
27
week4/ex2.py
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
def main():
|
||||||
|
print('==Exercise 2==')
|
||||||
|
print('formula: s=3.14 * (1+a/b)^3')
|
||||||
|
flag = True
|
||||||
|
while flag:
|
||||||
|
a = input("pls input number a:")
|
||||||
|
try:
|
||||||
|
a = int(a)
|
||||||
|
flag=False
|
||||||
|
except :
|
||||||
|
print("pls input a number!")
|
||||||
|
flag=True
|
||||||
|
while flag:
|
||||||
|
b = input("pls input number b:")
|
||||||
|
try:
|
||||||
|
b = int(b)
|
||||||
|
flag=False
|
||||||
|
except :
|
||||||
|
print("pls input a number!")
|
||||||
|
|
||||||
|
print(f"The result for S= {3.14*(1+(a+b)**3)}")
|
||||||
|
|
||||||
|
print('==End Of Ex2==')
|
||||||
|
return 0
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
||||||
26
week4/ex3.py
Normal file
26
week4/ex3.py
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
def main():
|
||||||
|
print('==Exercise 3==')
|
||||||
|
flag=True
|
||||||
|
while flag:
|
||||||
|
a = input("pls input first side length:")
|
||||||
|
try:
|
||||||
|
a = float(a)
|
||||||
|
flag=False
|
||||||
|
except :
|
||||||
|
print("pls input a float!")
|
||||||
|
flag=True
|
||||||
|
while flag:
|
||||||
|
b = input("pls input another side length:")
|
||||||
|
try:
|
||||||
|
b = float(b)
|
||||||
|
flag=False
|
||||||
|
except :
|
||||||
|
print("pls input a float!")
|
||||||
|
|
||||||
|
print(f"The lenth of the hypotenuse= {sum([i**2 for i in [a,b]])**0.5}")
|
||||||
|
|
||||||
|
print('==End Of Ex3==')
|
||||||
|
return 0
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
||||||
33
week4/ex4.py
Normal file
33
week4/ex4.py
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
def main():
|
||||||
|
from random import randint
|
||||||
|
print('==Exercise 4==')
|
||||||
|
LIST_A = [1,2.0,'c',{"id":4}]
|
||||||
|
LIST_B = [randint(1,100) for i in range(10)]
|
||||||
|
LIST_C = [*[chr(i+65) for i in range(10)],27,28.0]
|
||||||
|
|
||||||
|
print('Lists Initial Values:')
|
||||||
|
print(f'{i["idtf"]}: {i["value"]}' for i in [
|
||||||
|
{"idtf":"List A","value":LIST_A},
|
||||||
|
{"idtf":"List B","value":LIST_B},
|
||||||
|
{"idtf":"List C","value":LIST_C},
|
||||||
|
])
|
||||||
|
|
||||||
|
print('a range of items: ',LIST_C[:-3])
|
||||||
|
print('reverse print of list: ',LIST_C[::-1])
|
||||||
|
|
||||||
|
popped_item = LIST_C.pop(3)
|
||||||
|
print(f'Deleted 4th item(index=3)={popped_item} from list C: {LIST_C}')
|
||||||
|
|
||||||
|
LIST_C.insert(5,LIST_A)
|
||||||
|
print(f'Insert items from list A to list C start from pos5(index=4): {LIST_A}')
|
||||||
|
|
||||||
|
LIST_B.sort()
|
||||||
|
print('sorting List B:',LIST_B)
|
||||||
|
|
||||||
|
print('number of similar items in the list B: ',sum([1 if LIST_B[i] in LIST_B[i+1:] else 0 for i in range(len(LIST_B))]))
|
||||||
|
|
||||||
|
print('==End Of Ex4==')
|
||||||
|
return 0
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
||||||
12
week4/ex5.py
Normal file
12
week4/ex5.py
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
def main():
|
||||||
|
print('==Exercise 5==')
|
||||||
|
scores = [90,36,78,59,85,98,73,88,100,65]
|
||||||
|
scores.sort()
|
||||||
|
scores = scores[::-1]
|
||||||
|
print(scores,"\nMax score:",max(scores),"\nAvg score:",sum(scores)/len(scores))
|
||||||
|
|
||||||
|
print('==End Of Ex5==')
|
||||||
|
return 0
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
||||||
6
week4/run_all.py
Normal file
6
week4/run_all.py
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
import ex1,ex2,ex3,ex4,ex5
|
||||||
|
ex1.main()
|
||||||
|
ex2.main()
|
||||||
|
ex3.main()
|
||||||
|
ex4.main()
|
||||||
|
ex5.main()
|
||||||
Reference in New Issue
Block a user