Skip to content

added max trials#2

Open
olaoluwaidowu wants to merge 1 commit intomainfrom
assignment
Open

added max trials#2
olaoluwaidowu wants to merge 1 commit intomainfrom
assignment

Conversation

@olaoluwaidowu
Copy link
Copy Markdown
Collaborator

No description provided.

@olaoluwaidowu olaoluwaidowu requested a review from yusufom August 15, 2023 23:15
Copy link
Copy Markdown
Member

@yusufom yusufom left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A very nice approach to the task.
Well done.

# ask user for number
number = input("Enter the {}: ".format(position))

try:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I will make the corrections right away.

# call main function
if __name__ == "__main__":
main()
main() No newline at end of file
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noted. Thanks boss

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants