-1

I'm making a calculator on python for a school assignment but the trouble is, my teacher knows nothing. I've made the calculator, but I need to add some code so the user cannot input a number larger than 999, and if they do it creates a loop where the software asks what the users inputs are. Any help?

# Program make a simple calculator
# that can add, subtract, multiply
# and divide using functions

# define functions
def add(x, y):
   """This function adds two numbers"""

   return x + y

def subtract(x, y):
   """This function subtracts two numbers"""

   return x - y

def multiply(x, y):
   """This function multiplies two numbers"""

   return x * y

def divide(x, y):
   """This function divides two numbers"""

   return x / y

# take input from the user
print("Select operation.")
print("1.Add")
print("2.Subtract")
print("3.Multiply")
print("4.Divide")

choice = input("Enter choice(1/2/3/4):")

num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))

if choice == '1':
   print(num1,"+",num2,"=", add(num1,num2))

elif choice == '2':
   print(num1,"-",num2,"=", subtract(num1,num2))

elif choice == '3':
   print(num1,"*",num2,"=", multiply(num1,num2))

elif choice == '4':
   print(num1,"/",num2,"=", divide(num1,num2))
else:
   print("Invalid input")

I've tried a variety of things but nothing works P.S. I'm a bit of a novice so go easy Thanks

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
  • You already have the required building blocks. `if` num is bigger than 999, ask the user to enter it again. You can use `while` to execute a block of code (hint hint: the input code) as long as a condition is true. – toasted_flakes Jun 15 '14 at 14:28

2 Answers2

2
while True:
    num = int(raw_input('enter an integer <= 999: '))
    if num <= 999:
        break
    print('invalid input, please try again!')

print('you entered: {}'.format(num))
timgeb
  • 76,762
  • 20
  • 123
  • 145
  • When I add the code, it just keeps asking for numbers to input- choice = input("Enter choice(1/2/3/4):") num1 = int(input("Enter first number: ")) num2 = int(input("Enter second number: ")) while True: num = int(raw_input('enter a number: ')) if num <= 999: break- Output= Enter choice(1/2/3/4):2 Enter first number: 9999999 Enter second number: 9999999 enter a number: 9999999999999999 enter a number: 9999999999999999 enter a number: 999999999999999 enter a number: 99999999999999999 Sorry, I'm a novice – user3742334 Jun 15 '14 at 14:40
  • @user3742334 the code is supposed to run forever until the user enters a valid number. I have added some print statements to the demo above to make this clear. Also, remember to adjust any variable names from the demo to match your actual code. – timgeb Jun 15 '14 at 14:54
2
MAX_VALUE = 999

def read_number(message):
    n = MAX_VALUE
    while (n >= MAX_VALUE):
        n = int(raw_input(message))
    return n

num1 = read_number("Enter first number:")
num2 = read_number("Enter second number:")

Example run:

Enter first number: 1000
Enter first number: 56
Enter second number: 800
John Jaques
  • 560
  • 4
  • 17
  • I have edited my code using your code and changing the variables. The loop works, but when I enter 2 valid numbers, it outputs 'Invalid Input' when it should output the answer- num1 = int(input("Enter first number: ")) num2 = int(input("Enter second number: ")) MAX_VALUE = 999 def read_number(message): num1 = MAX_VALUE while (num1 >= MAX_VALUE): num1 = int(raw_input(message)) return num1 – user3742334 Jun 16 '14 at 17:22
  • The message "Invalid input" is printed when the choice is invalid, not the operands. Make sure that you input the right number at the right time. You are probably inputting an operand when you should actually be inputting the choice. – John Jaques Jun 16 '14 at 18:13