0
[SOLVED] Repeating a break command
I’ve been working on a robot in python and need to use a break command to prevent the outcome from looping. Is there any way I can make it so the break isn’t mandatory and I can have it there so when the command runs it automatically breaks it so I don’t have to manually break every single thing https://code.sololearn.com/cGBTjfDicqdd/?ref=app
3 Respostas
+ 5
Your method of tackling it doesn't look very efficient to me and I don't think it will scale well.
There are few things you can do to improve it :
1) instead of using a massive if-else tree, you can simply use a data structure ( like a dictionary in python ) to map the inputs to their corresponding outputs
2) instead of looking for all the possibilities user can type a single sentence, it's much better to convert the entire input string to lowercase ( or uppercase, whatever suits you ) and then evaluate it.
3) make use of the fact that functions can return stuff, so instead of writting functions that print stuff on the screen, it's much better to let them return the required string to the caller and let the caller take care of printing.
+ 1
1) Take Arsenic's advice.
2) Until then, your code could be reduced a little by removing all the breaks and keeping only the one at the end (line 154) and you would get the same outcome.
3) Lines 152-153 look like they don't do what you intended. Try changing from this:
else: user_input == (user_input)
print("no response found.")
break
To this:
else:
print("no response found.")
continue
break
Notice the continue statement is there to continue the loop, whereas if valid input is found then the if/elif/else statement proceeds to the break.