0

Why is this "else statement" not working?

Instructions on how to use the code: 1. The real code has options for celsius to fahr, fahr to cels, cels to kelv, ETC., for simplicity I limit the code provided to Cels to Fahr. 2. Input like this "x-degree/100 c to f". "34.141512 c to f" 3. Program checks to 1) round the answer to two decimal places, 2) if it is a whole number it removes decimal point from printed integer. 3) it is SUPPOSED to check input for an "s". If "s" exist in input it should print an EXACT decimal. e.g. "94 c to f s" should print "202.20000002", but it doesn't. import re x = input("Degree c to f: ").lower() y = x if re.sub('[1234567890. ]', '', x) == "ctof": x = re.sub('[to cfks]', '', x) r = (9 / 5 * float(x) + 32) if r % 1 == 0: print(int(r)) # If it is a whole number it will remove the decimal point. elif r % 1 != 0: print(round(r, 2)) # rounded to two decimal places. else: x = re.sub("[1234567890. ctokf]", "", x) # this is designed so if 's' is in the input the program returns an EXACT answer if x == "s": print(float(r)) I have been working on this code for too long, so long that I dug myself into figuring out why it won't work ( I rechecked and rewrote everything ten times).....I need help.

19th Jun 2019, 6:51 AM
Paras finn
Paras finn - avatar
2 Respuestas
+ 2
Interesting way to use regex. I'll just drop this link as I'm on my way to work: https://code.sololearn.com/csS4NDz9doOL/?ref=app Maybe it helps. If it doesn't, I'll see if I can help you later
19th Jun 2019, 6:57 AM
Anna
Anna - avatar
+ 1
@anna Though you didn't directly give me the answer, that was quite encouraging to me. Uusually i hear essentially 'you're a f**king retard' and I quit. An yway yes it was quite simple, 1) , I used bad indentation technique, 2) I forgot to use the y = x variable I had saved like a complete Alzheimer's patient...(no offense ) Here what it ended up looking like, (of course before i put it back in a function...) import re x = input("Degree c to f: ").lower() y = x if re.sub('[1234567890. ]', '', x) == "ctof": x = re.sub('[to cfks]', '', x) r = (9 / 5 * float(x) + 32) if r % 1 == 0: print(int(r)) # If it is a whole number it will remove the decimal point. elif r % 1 != 0: print(round(r, 2)) # rounded to two decimal places. y = re.sub('[1234567890.cfkto ]', '', y) x = re.sub('[cfktos ]', '', x) r = (9 / 5 * float(x) + 32) # this is designed so if 's' is in the input the program returns an EXACT answer if y == "s": print(float(r))
19th Jun 2019, 7:08 AM
Paras finn
Paras finn - avatar