So simple yet not working.... - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: So simple yet not working.... (/thread-30579.html) Pages:
1
2
|
So simple yet not working.... - giladal - Oct-26-2020 Hi Guys. I am trying to remove the letter F or C or f or c from the string and then print it. Here is the code, and for the love of god, I don't understand what I am doing wrong. Its printing exactly what i type in 32F or 25C... temperature = (input("Insert temperature: ")) if "f" in temperature or "F" in temperature: new_string = temperature.replace("F", "") new_string = temperature.replace('f', '') print(new_string) else: new_string = temperature.replace("c", "") new_string = temperature.replace('C', '') print(new_string) RE: So simple yet not working.... - Askic - Oct-26-2020 When execute your code, I get this: Insert temperature: 35C 35 Insert temperature: 25f 25 So it appears it works, but the general approach is not that good. What if user enters 27B? RE: So simple yet not working.... - bowlofred - Oct-26-2020 Line 5 (and line 10) doesn't do anything. Regardless of the contents of new_string after that line is run, it will be overwritten when line 6 is executed. probably easiest to not create "new_string" and just do the replacements on "temperature". Or move the string to uppercase and do a single replacement. RE: So simple yet not working.... - giladal - Oct-26-2020 (Oct-26-2020, 10:37 PM)Askic Wrote: When execute your code, I get this: Thank you very much Askic. I haven't gotten to that yet. :) That is very weird that it works for you and it doesn't work for me. RE: So simple yet not working.... - Askic - Oct-26-2020 I'd do something like this: temperature = input("Insert temperature: ") print(temperature.translate({ord(i): None for i in 'FCfc'})) RE: So simple yet not working.... - giladal - Oct-26-2020 I re-wrote it with your suggestion. Still I get the F letter attached... temperature = (input("Insert temperature: ")) if "f" in temperature or "F" in temperature: temperature = temperature.upper() temperature.replace("F", "") print(temperature) RE: So simple yet not working.... - giladal - Oct-26-2020 (Oct-26-2020, 10:41 PM)giladal Wrote:(Oct-26-2020, 10:37 PM)Askic Wrote: When execute your code, I get this: Not supposed to use things I haven't learned yet. It is part of a course I am taking online. :) Thanks though. RE: So simple yet not working.... - bowlofred - Oct-26-2020 In your changed version, on line 5: temperature.replace() doesn't modify temperature. It returns the modified string. If you don't assign it to something (like temperature), the replacement is lost. temperature.replace("F", "") # this is a noop because the replaced string is not captured anywhere RE: So simple yet not working.... - giladal - Oct-27-2020 (Oct-26-2020, 11:10 PM)bowlofred Wrote: In your changed version, on line 5: temperature.replace() doesn't modify temperature. It returns the modified string. If you don't assign it to something (like temperature), the replacement is lost. BINGO! Now it works. So now I understand that I can't just add the .replace to a str and it will replace the character. I have to assign it to the variable. Thanks a million bowlofred!! RE: So simple yet not working.... - jps2020 - Oct-27-2020 I'm sorry to say it doesn't work for me, and I'm not sure why. This is my code, where I've replaced Line 5 with your suggestion, bowlofred: temperature = (input("Insert temperature: ")) if "f" in temperature or "F" in temperature: temperature = temperature.upper() temperature.replace("F", "") # this is a noop because the replaced string is not captured anywhere print(temperature) When I run it, I'm asked to input temperature. I input 32F. It then prints: 32F So there's nothing being replaced. |