Carl123432 Jan 18th, 2019 64 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
- # Today we are going to create a simple calculator in python.
- # We are going to use while loops in our code today. The while loop is a control flow statement that allows code to be executed repeatedly based on a given Boolean condition. The while loop can be thought of as a repeating if statement.
- while True: # We have made a while True loop. which will loop until it is told to break out of the loop
- enter = input('Enter the first number: ') # Asked user to insert there first number
- enter1 = input('Enter the mathmatical operator *+/-: ') # Asked user to insert which mathatical Operation they would like.
- enter2 = input('Enter second number: ') # Asked user for third number.
- if enter1 in ['*', 'x', 'X']: # Now we are reading the user input and if it includes a piece of text that matches our list. We call the function to times the 2 numbers and we do the same for all 4.
- total_sum = int(enter) * int(enter2)
- elif enter1 == '+':
- total_sum = int(enter) + int(enter2)
- elif enter1 == '/':
- total_sum = int(enter) / int(enter2)
- elif enter1 == '-':
- total_sum = int(enter) - int(enter2)
- elif enter1 in ['stop', 'Stop', 'STOP']: # Now if the user types stop, it will send them to this statment which asks them if thy want to quit.
- stop = input('Are you sure you want to stop the program?: ')
- if stop in ['yes', 'YES', 'y', 'Y']:
- break # If it is a yes, we use the break function. which will return the loop false. and stop the program.
RAW Paste Data