39 lines
1.2 KiB
Python
39 lines
1.2 KiB
Python
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() |