inversecasse.py

Created by schraf

Created on September 07, 2018

619 Bytes

Écrire une fonction “inverse” qui admet une phrase en paramètre (par exemple “Python For ever !”) et qui en sortie affiche cette même phrase où les minuscules sont devenues des majuscules et vice-versa. Les autres lettres (apostrophes, ponctuations, guillemets etc) ne changent pas.

Pour notre exemple, le programme affichera donc “pYTHON fOR EVER !”


def inverse(phrase):
  sortie=""
  for c in phrase:
      if c==c.upper():
          sortie+=c.lower()
      else:
          sortie+=c.upper()
  return sortie
  
# Version 2
def inverse2(phrase):
  sortie=""
  for c in phrase:
      sortie+=c.lower() if c==c.upper() else c.upper()
  return sortie
  
# Version 3
def inverse3(phrase):
  sortie=""
  for c in phrase:
    if "A" <= c <= "Z":
        sortie+=chr(ord(c)+32)
    elif "a" <= c <= "z":
        sortie+=chr(ord(c)-32)
    else:
        sortie+=c
  return sortie
  
# phrase.swapcase() serait le plus court
# mais pas present sur la machine

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our <a href="https://www.numworks.com/legal/cookies-policy/">cookies policy</a>.