online compiler and debugger for c/c++

code. compile. run. debug. share.
Source Code    Language
''' Short demonstration for showing exception handling in Python keywords: raise, isinstance, isnumeric, try/except, 11/2/2021 -Dr. G ''' ''' Classes of errors include: syntax, convention, logic, and runtime Describe each of these error types and give an example of each. Syntax: Convention: Logic: Runtime: ''' ''' Runtime errors are unpredictable errors that happen at runtime. If we don't, handle the error it will be up to "python" to handle it. Letting "python" handle it is bad programing practice, but we'll get to that. You've already seen exceptions when you are trying to run your program. Run this program and figure out the error I left in the code. You can also intentionally raise an error with the keyword "raise." Existing exceptions to raise: Examples: AttributeError ImportError IndexError NameError TypeError ValueError ZeroDisionError Check out the first couple lines of Main. Notice the Woot. Always make sure this program prints that Woot.... ''' from functions import * def main(): ''' Part 1 : throwing your own runtime errors In your groups examine the below function and discuss what all could go wrong. Don't let Python deal with the problem. Create and throw more meaningful errors. Fix it so that "Woot" will print even if an error occurs. What does this represent? What's the advantages to creating our own exception? Move on to part 2.... ''' #print(divide(4,16)) #print(divide(4,0)) #print(divide('4','16')) ''' Part 2 : don't stop the control flow Look at the function being used below and consider what could go wrong. Throw an appropriate error with appropriate messages for each of the below situations. ''' #try one at a time #num_list1 = [1,2,3,4] #num_list2 = {1,2,3,4} #num_list3 = 999 #zeroOff(num_list1) #fix it so "Woot" at the bottom of this program will print regardless ''' Part 3 : something a little more complicated Consider this below function and add appropriate error handling Don't let the program come back to main on incorrect input Hint: take advantage of the function "int" throwing an exception and think back to the break and continue commands ''' #guessing_game(1,5) print("Woot") main()
import random #simple function to demonstrate raising an error with your own message def divide(x,y): #simple method, but what could go wrong? return x/y #simple method to demonstrate isinstance checking def zeroOff(num_list): for i in range (len(num_list)): num_list[i] = 0 #simple method to demonstrate control flow issues def get_user_input(): num = -1 #What all could go wrong? num = int(input("Please enter a number from 0 - 10 :")) return num #number guessing game def guessing_game(range1, range2): num = random.randint(range1, range2) while True: guess = int(input("please guess a number between " + str(range1) + " and " + str(range2) + " ")) if guess > num: print("too high") elif guess < num: print("too low") else: print("You win!") break

Compiling Program...

Command line arguments:
Standard Input: Interactive Console Text



Program is not being debugged. Click "Debug" button to start program in debug mode.