+ 3

Why are my If statements ignored here?

I'm rather new to programming, and I'm enjoying this app significantly. However, I figure the best way that I'm going to learn, is if I write something from scratch, featuring one of the examples of things I learned. One such example was a "Simple Calculator", something of a big step for me. I worked out all of the errors I was getting in about a 24 hour peroid, but now my calculator will only ever "divide" any two numbers I give it. I had set up some If statements to try and thin down any possibility for error, but my If statements are being executed anyway, as if coming back as True. Why? I can't seem to figure it out. Replacing the If statements with Elif statements causes the opposite problem. The lines of code I'm referring to are on lines 21, 25, 29, and 33. that may change if I update this code trying some of your suggestions. Any help at all is appreciated! Even "hints" as opposed to straight up answers or solutions. Thanks for all your help! https://code.sololearn.com/ckBv6fnh0sJK/?ref=app

15th Aug 2018, 1:04 AM
mrhonk123
mrhonk123 - avatar
4 ответов
+ 17
For each thing you want to check input_modifier against, you have to write it again. Ex: if input_modifier == "add" or input_modifier == "Add" or input_modifier == "addition" or input_modifier == "Addition": input_modifier = "+" Any string on its own evaluates as true, which is why your code always runs. So, for example, if 'input_modifier == "add"' is false, the string "Add" on its own is considered true, and the code will run anyway. By the way, to shorten your code a little, you can change input_modifier to all lowercase like this: input_modifier = input_modifier.lower()
15th Aug 2018, 1:12 AM
Tamra
Tamra - avatar
+ 10
just a general hint (not related to the question) put all valid input in a variable, i.e. addition = ("add", "Add", "addition", "Addition", "+",) if input_modifier in addition: input_modifier = "+" to save a lot of lines
15th Aug 2018, 1:13 AM
LONGTIE👔
LONGTIE👔 - avatar
+ 9
mrhonk123 same here
15th Aug 2018, 1:22 AM
LONGTIE👔
LONGTIE👔 - avatar
+ 2
Wow! Thank you guys! Looks like I've got a lot to learn.
15th Aug 2018, 1:20 AM
mrhonk123
mrhonk123 - avatar