File tree Expand file tree Collapse file tree 1 file changed +13
-1
lines changed Expand file tree Collapse file tree 1 file changed +13
-1
lines changed Original file line number Diff line number Diff line change @@ -136,7 +136,14 @@ def drive(inputString):
136136 return
137137
138138 direction = inputList [0 ].lower () # Get the first word and convert it to lowercase (so we can accept 'f' or 'F')
139- distance = int (inputList [1 ]) # Get the second word and convert it to an integer
139+ # Code that we put in the "try" block might cause an error, so we can catch that error with "except" and handle it gracefully
140+ # For example if the user enters something weird for distance like "ten" instead of "10", the int() function will throw an error
141+ # and we can catch that error and print a message instead of the program crashing
142+ try :
143+ distance = int (inputList [1 ]) # Get the second word and convert it to an integer
144+ except :
145+ print ("Invalid distance. Please enter a valid number." )
146+ return
140147
141148 # check the switch state and only drive if the switch is pressed
142149 if switchPin .value () == 1 : # If the switch is in OFF position
@@ -171,3 +178,8 @@ def drive(inputString):
171178# print("Executing command:", c)
172179# drive(c) # Execute each command in the list
173180# sleep(1) # Add a delay between commands to see what's happening more clearly
181+
182+ # The below code will run forever and allow us to enter further commands interactively!
183+ while True :
184+ c = input ("Enter a new command!: " ) # Get user input in the c variable in the form of "f 10" or "r 90"
185+ drive (c )
You can’t perform that action at this time.
0 commit comments