Advertisement
Hugo_2000

4. loops

Jun 27th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.80 KB | None | 0 0
  1. # calculator that tests input and keeps adding for ever
  2. action = input("Insert maths sign (+,-,*,/) = ");
  3. num1 = int(input("please input number = "));
  4. loop = 0;
  5.  
  6. # loop until a non number is entered
  7. while loop == 0:
  8.     numX = input("please input number (press enter to end)= ");
  9.  
  10.     # tests if a number was input
  11.     if numX.isdigit():
  12.         # does maths
  13.         if action == "+":
  14.             num1 = num1 + int(numX);
  15.         elif action == "-":
  16.             num1 = num1 - int(numX);
  17.         elif action == "/":
  18.             num1 = num1 / int(numX);
  19.         elif action == "*":
  20.             num1 = num1 * int(numX);
  21.         else:
  22.             # wrong input
  23.             print("Need a correct maths sign (+,-,*,/)")
  24.             loop = 1;
  25.     else:
  26.         loop = 1;
  27.         print("=", num1);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement