Jun-17-2019, 12:46 PM
Hello All,
I am stumbling into the world of programming, and have started by watching videos, and reading beginner instructions on Python. I have just started with Hello World, and using an online lessons, a basic calculator script. I have ventured off and wondering if I can make it a little more functional and what does what.
Below is the code, the question is, how would you make it, that the user has to select the pre-defined options, and if not, it gives an error message like, try again selecting from he above options? Right now the script will run to the end until the error comes back. What if you wanted to force the input to be a number, and the same again, if not, error message and start again?
Lastly, can you make the response ignore the capitalisation - so "Multiply" or "multiply" is acceptable?
I do appreciate this is basic stuff, so be kind :)
I am stumbling into the world of programming, and have started by watching videos, and reading beginner instructions on Python. I have just started with Hello World, and using an online lessons, a basic calculator script. I have ventured off and wondering if I can make it a little more functional and what does what.
Below is the code, the question is, how would you make it, that the user has to select the pre-defined options, and if not, it gives an error message like, try again selecting from he above options? Right now the script will run to the end until the error comes back. What if you wanted to force the input to be a number, and the same again, if not, error message and start again?
Lastly, can you make the response ignore the capitalisation - so "Multiply" or "multiply" is acceptable?
I do appreciate this is basic stuff, so be kind :)
number1 = int(input("What is the first number?")) operator = input("What do you want to do + Plus - Minus * Multiply / Divide or p Power of? ") number2 = int(input("What is the second number?")) if operator == "+": answer = number1+number2 elif operator == "-": answer = number1-number2 elif operator == "*": answer = number1*number2 elif operator == "/": answer = number1/number2 if operator == "Plus": answer = number1+number2 elif operator == "Minus": answer = number1-number2 elif operator == "Multiply": answer = number1*number2 elif operator == "Divide": answer = number1/number2 elif operator == "p": answer = number1**number2 elif operator == "power of": answer = number1**number2 print("The answer is " + str(answer))