Conversation
yusufom
left a comment
There was a problem hiding this comment.
A very nice approach to the task.
Well done.
| # ask user for number | ||
| number = input("Enter the {}: ".format(position)) | ||
|
|
||
| try: |
There was a problem hiding this comment.
this code block looks redundant.
You can try cast the input in line 36.
here is a cleaner approach that eliminates the while loop and provides better error handling
def get_number(position, max_trials=5):
for _ in range(max_trials):
try:
number = float(input(f"Enter the {position}: "))
return number
except ValueError:
print("Invalid input. Please enter a valid number.")
print(f"{max_trials} Trials Exceeded! Program Ended.")
return None
There was a problem hiding this comment.
Thanks! I will make the corrections right away.
| # call main function | ||
| if __name__ == "__main__": | ||
| main() | ||
| main() No newline at end of file |
There was a problem hiding this comment.
The code stops after every succesful execution which is alittle less effective.
You can ask if the user wants to continue or do another calculation, if yes...the program re-runs, if no, the program stops.
here is a basic approach to it ,
def main():
cont = True
while cont:
first_number = get_number("first number")
if first_number is None:
return
second_number = get_number("second number")
if second_number is None:
return
operation, symbol = get_operation()
result = execute(operation, first_number, second_number)
print(f"Result: {first_number} {symbol} {second_number} = {result}")
cont_input = input("Would you like to perform another operation? yes or y to continue and no or n to stop :")
if cont_input == "yes" or cont_input == "y":
continue
else:
print("Have a great day")
cont = False
There was a problem hiding this comment.
Noted. Thanks boss
No description provided.