මෙම පාටමාලාව තුල ඇති ප්රදානම Assignment එක වන calculator programming සදහා මෙහි උපකාරක කේත දක්වා ඇත.
For Addition (+) එකතු කිරීම සදහා පමණක් සැකසු
last_calculation = []
def add(a, b):
global last_calculation
print(a + b)
resultformat = "{0} + {1} = {2}".format(str(a), str(b), str(a+b)) #{} place holders, .format- make specific statements
last_calculation.append(resultformat)
def history():
global last_calculation
if last_calculation == []:
print("no last calculations")
else:
for i in last_calculation:
print(i)
while True:
print("Select operation.")
print("1.Add : + ")
print("2.Terminate: # ")
print("3.Reset : $ ")
print("4.History : ? ")
choice = input("Enter choice + / # / $ / ? -")
print(choice)
if (choice == '#'):
print("Done. Terminating")
exit()
elif (choice in ('+','$','?')):
if choice == '+':
num1s = input("Enter first number: ")
num2s = input("Enter second number: ")
num1 = float(num1s)
num2 = float(num2s)
add(num1, num2)
elif choice == '$':
print("Reset")
continue
elif choice == '?':
history()
else:
print("not a correct input")
Choice තේරීම
def select_op(choice):
if (choice == '#'):
return -1
elif (choice == '$'):
return 0
elif (choice == '?'):
return 1
while True:
choice = input("Enter choice # / $ / ?-")
print(choice)
if(select_op(choice) == -1): #program ends here
print("Done. Terminating")
exit()
elif(select_op(choice) == 0):
print("Play again")
elif(select_op(choice) == 1):
print("Print History")
else:
print("Your input is not # / $ / ?")
Chioce In from Given ලබා දී ඇති ඒවා අතරින් තේරීම
choice = input("Enter choice")
if (choice == '#'):
print("exit")
elif (choice in ('+','-','*','/','^','%','?')):
print("correct input")
else:
print("not a correct input")
While TRUE වැඩසටහන නොනවත්වා පවත්වා ගැනීම
while (True):
num2s = input("Enter $ or #:")
print(num2s)
if num2s.endswith('$'):
print("You entered $")
elif num2s.endswith('#'):
print("You entered #")
else:
print("You entered another")
No comments:
Post a Comment