Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions calculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,16 @@ def execute(operation,x,y): # function to execute operation

def get_number(position, repeat=True): # function to request for user's input

count = 0
while repeat:

# track and return trials exceeded after 5 times
count += 1
if count == 6:
print("\n5 Trials Exceeded!!! Program Ended\n\n.... Try again later and enter a number\n")
return "Trials Exceeded"

# 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.

Expand Down Expand Up @@ -54,10 +63,21 @@ def get_operation(): # function to request operation type from user

def main():

# get numbers into variables
# get first number into variable
first_number = get_number("first number")

# end after trials exceeded for first number
if first_number == "Trials Exceeded":
return None

# get second number into variable
second_number = get_number("second number")

# end after trials exceeded for second number
if second_number == "Trials Exceeded":
return None


# get operation number and operation symbol
operation, symbol = get_operation()

Expand All @@ -70,4 +90,4 @@ def main():

# call main function
if __name__ == "__main__":
main()
main()
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