+ 1

Can someone check if my code works on pc version of python. It's not workin on my phone compiler but I'm confident with my code

https://code.sololearn.com/c46po9O0wSao/?ref=app

24th Apr 2018, 9:42 AM
Jefferson Asendente
Jefferson Asendente - avatar
4 ответов
+ 2
It's great. I added some new lines to make it look clearer on the command-line. It's also clearer to ask them to specify 'add' and not 'Add' etc, because your string equality tests, don't check for case-insensitive, i.e. if they DO enter 'Add', it will say "Invalid Choice". while True: print("\nWelcome to my first program.") print("This is my python arithmetic calculator.") print("\nChoose an Operation:\n> add\n> subtract\n> multiply\n> divide") print("\nEnter quit to exit.\n") user_input = input(": ") if user_input == "quit": break elif user_input == "add": num1 = float(input("Enter num1: ")) num2 = float(input("Enter num2: ")) result = str(num1 + num2) print("\nThe result is "+ result) elif user_input == "divide": num1 = float(input("Enter num1: ")) num2 = float(input("Enter num2: ")) result = str(num1 / num2) print("\nThe result is "+ result) elif user_input == "subtract": num1 = float(input("Enter num1: ")) num2 = float(input("Enter num2: ")) result = str(num1 - num2) print("\nThe result is "+ result) elif user_input == "Multiply": num1 = float(input("Enter num1: ")) num2 = float(input("Enter num2: ")) result = str(num1 * num2) print("\nThe result is "+ result) else: print("Invalid Choice") break $ python3 test.py Welcome to my first program. This is my python arithmetic calculator. Choose an Operation: > add > subtract > multiply > divide Enter quit to exit. : add Enter num1: 1 Enter num2: 2 The result is 3.0 Welcome to my first program. This is my python arithmetic calculator. Choose an Operation: > add > subtract > multiply > divide Enter quit to exit. : divide Enter num1: 3 Enter num2: 0 Traceback (most recent call last): File "test.py", line 18, in <module> result = str(num1 / num2) ZeroDivisionError: float division by zero Also note that, if you divide a number by zero it errors, so you might want to catch for that
24th Apr 2018, 12:53 PM
Emma
+ 2
Here's a link to the updated code for you to check: https://code.sololearn.com/c46po9O0wSao/?ref=app#py Note, you can also work on not duplicating code. print("\nThe result is "+ result) should be only run in one place. Either put it in a function, and call it that way, or bring it outside all the if.. then... else... structures somehow. Duplication of code is bad, because if you wanted to change: print("\nThe result is "+ result) to: print("\nThe calculated result is "+ result) You'd have to edit all those lines. If you didn't the output would be inconsistent. Also, if you had to edit it in multiple areas of code, you are more likely to introduce an error! Check the comments at the top of my code, listing issues so far: # Issues with code: # 1) Case insensitive checks for the operation (add, subtract etc) # 2) Duplication of code [print("\nThe result is "+ result)] # 3) Not checking for divide by zero # 4) Not validating user input: # if you enter 'fred' as a number, you get an error: # ValueError: could not convert string to float: 'fred' # It's better to tell the user a decent warning message. However, this is a brilliant first program! :-)
24th Apr 2018, 12:55 PM
Emma
+ 2
Happy coding ☺
24th Apr 2018, 1:44 PM
Emma
+ 1
Thank you so much sir! I will follow your tips and suggestions ^__^
24th Apr 2018, 1:29 PM
Jefferson Asendente
Jefferson Asendente - avatar