-
Notifications
You must be signed in to change notification settings - Fork 0
added max trials #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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: | ||
|
|
@@ -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() | ||
|
|
||
|
|
@@ -70,4 +90,4 @@ def main(): | |
|
|
||
| # call main function | ||
| if __name__ == "__main__": | ||
| main() | ||
| main() | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 ,
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Noted. Thanks boss |
||
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.