Skip to content

Commit cd05fd7

Browse files
Add more fun continuous input for users on example 5b in fw
1 parent ca825d2 commit cd05fd7

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

source_only_examples/project5_b.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff 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)

0 commit comments

Comments
 (0)