4 Replies - 226 Views - Last Post: 19 October 2013 - 02:16 AM Rate Topic: -----

#1 codeblox  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 18-October 13

Need clarification on if x in y: statement

Posted 18 October 2013 - 04:46 PM

I have written the following code:


#! /usr/bin/env python3.3                                                                    

msg = "g fmnc wms bgblr rpylqjyrc gr zw fylb. rfyrq ufyr amknsrcpq ypc dmp. bmgle gr gl zw f\
ylb gq glcddgagclr ylb rfyr'q ufw rfgq rcvr gq qm jmle. sqgle qrpgle.kyicrpylq() gq pcamkkcl\
bcb. lmu ynnjw ml rfc spj. "

done = 0
new_msg = list(msg)

rep_chars = {"k":"%", "o":"q", "e":"g"}

for old, new in rep_chars.items():
    print("Checking for: ", old)
    if old in msg:
        print("Found old: ", old)
        msg = msg.replace(old, new)

print(msg)





It works as expected and replaces all a specified character with another. What I am curios about is the if old in msg: line.

Origionally, I had it as

while old in msg:


since I figured that's what I needed but
if old in msg:
appears to be doing the same thing. How is that possible?

Is This A Good Question/Topic? 0
  • +

Replies To: Need clarification on if x in y: statement

#2 codeblox  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 18-October 13

Re: Need clarification on if x in y: statement

Posted 18 October 2013 - 04:55 PM

Nevermind...

[quote]

str.replace(old, new[, count])

Return a copy of the string with all occurrences of substring old replaced by new. If the optional argument count is given, only the first count occurrences are replaced.
[/code]
Was This Post Helpful? 0
  • +
  • -

#3 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5777
  • View blog
  • Posts: 12,592
  • Joined: 16-October 07

Re: Need clarification on if x in y: statement

Posted 18 October 2013 - 04:57 PM

The behavior would be identical if the number of occurrences is zero or one. Think about it.

if is_true:
    code # none or once if is_true

while is_true:
    code # none or or until is_true is false


Was This Post Helpful? 0
  • +
  • -

#4 codeblox  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 18-October 13

Re: Need clarification on if x in y: statement

Posted 18 October 2013 - 08:46 PM

Absolutely. Additionally... Why is there no edit feature on these fourms for posts with typos???
Was This Post Helpful? 0
  • +
  • -

#5 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5777
  • View blog
  • Posts: 12,592
  • Joined: 16-October 07

Re: Need clarification on if x in y: statement

Posted 19 October 2013 - 02:16 AM

New accounts don't have edit, yet. Hang around a bit and you'll be able to.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1