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)
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?
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.
(Oct-26-2020, 10:37 PM)Askic Wrote: [ -> ]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?
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.
![[Image: z5CwZxb]](https://ibb.co/z5CwZxb)
I'd do something like this:
temperature = input("Insert temperature: ")
print(temperature.translate({ord(i): None for i in 'FCfc'}))
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)
(Oct-26-2020, 10:41 PM)giladal Wrote: [ -> ] (Oct-26-2020, 10:37 PM)Askic Wrote: [ -> ]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?
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.
![[Image: z5CwZxb]](https://ibb.co/z5CwZxb)
Not supposed to use things I haven't learned yet. It is part of a course I am taking online. :)
Thanks though.
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
(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.
temperature.replace("F", "") # this is a noop because the replaced string is not captured anywhere
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!!
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.