Python Forum
So simple yet not working....
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
So simple yet not working....
#1
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)
Reply
#2
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?
Reply
#3
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.
Reply
#4
(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]
Reply
#5
I'd do something like this:
temperature = input("Insert temperature: ")

print(temperature.translate({ord(i): None for i in 'FCfc'}))
Reply
#6
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)
Reply
#7
(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]

Not supposed to use things I haven't learned yet. It is part of a course I am taking online. :)

Thanks though.
Reply
#8
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
Reply
#9
(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!!
Reply
#10
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Simple conditional not working as expected return2sender 8 1,010 Aug-27-2023, 10:39 PM
Last Post: return2sender
  Why isn't this working. I've made it as simple as posible gcryall_Forum 3 2,425 Mar-10-2019, 03:07 PM
Last Post: gcryall_Forum

Forum Jump:

User Panel Messages

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