Python Forum
Beginner Question - Esaping the Escape Character correctly?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Beginner Question - Esaping the Escape Character correctly?
#1
I am working on some basic programming homework, a calculator. The code is below.

I would like for the user to be able to input / or \ for division. However, the code below does not work. If I remove the or "\\", the program runs fine. Any guidance is appreciated!

print("Acceptable operators include *, /, \, +, -")
math_num1 = input("Please enter the first number: ")
math_oper = input("Please enter the operator: ")
math_num2 = input("Please enter the second number: ")

if math_oper == "*":
    print(int(math_num1) * int(math_num2))
elif math_oper == "/" or "\\":
    print(int(math_num1) / int(math_num2))
elif math_oper == "+":
    print(int(math_num1) + int(math_num2))
elif math_oper == "-":
    print(int(math_num1) - int(math_num2))
else:
    print("Invalid Input! Please try again.")
Reply
#2
read https://python-forum.io/Thread-Multiple-...or-keyword

the problem is on line 8, how you use/expect or to work
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Note that you should also escape the backslash on the first line, when you print the instructions.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#4
I observe that in else: clause you try to validate only operator. Code has no defence against user entering value which cannot be converted into int.

For 'calculator' it's more conventional to use dictionary of operators, you can save lot of typing.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#5
Thank you! The program is now working. Strangely I did not have to escape the slash in the instructions... I'm not asking questions.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Very Beginner question on simple variables Harvy 1 188 Apr-12-2024, 12:03 AM
Last Post: deanhystad
  How do I handle escape character in parameter arguments in Python? JKR 6 1,175 Sep-12-2023, 03:00 AM
Last Post: Apoed2023
  use of escape character in re.sub and find WJSwan 1 916 Feb-16-2023, 05:19 PM
Last Post: Larz60+
  A simple "If...Else" question from a beginner Serena2022 6 1,731 Jul-11-2022, 05:59 AM
Last Post: Serena2022
Question Beginner Boolean question [Guessing game] TKB 4 2,330 Mar-22-2022, 05:34 PM
Last Post: deanhystad
  Escape indentation Frankduc 11 3,083 Jan-31-2022, 02:41 PM
Last Post: Frankduc
  add Escape charcters in string GrahamL 3 1,176 Jan-20-2022, 01:15 PM
Last Post: GrahamL
  Beginner question NameError amazing_python 6 2,465 Aug-13-2021, 07:28 AM
Last Post: amazing_python
  [solved] unexpected character after line continuation character paul18fr 4 3,419 Jun-22-2021, 03:22 PM
Last Post: deanhystad
  Escape Single quotation between each content tag usman 3 2,812 May-02-2021, 03:32 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020