Advertisement
sweeneyde

Assignment Expressions in Python 3.8

May 27th, 2019
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.52 KB | None | 0 0
  1. import re
  2.  
  3. # Python 3.7 #########################
  4.  
  5. cmd = input("Enter a command, or just press enter to quit: \n> ")
  6. while cmd:
  7.    
  8.     match = re.match(r"Alexa, play ((\w+\s)*\w+)\.", cmd)
  9.     if match:
  10.         song_name = match.group(1)
  11.         print(f"Playing {song_name}...")
  12.        
  13.     else:
  14.         match = re.match(r"Alexa, dim the lights in ((\w+\s)*\w+)\.", cmd)
  15.         if match:
  16.             room_name = match.group(1)
  17.             print(f"Dimming the lights in {room_name}...")
  18.            
  19.         else:
  20.             match = re.match(r"Alexa, call ((\w+\s)*\w+)\.", cmd)
  21.             if match:
  22.                 name = match.group(1)
  23.                 print(f"Calling {name}...")
  24.                
  25.             else:
  26.                 print(f"I don't understand.")
  27.                
  28.     cmd = input("Enter a command, or just press enter to quit: \n> ")
  29.            
  30.  
  31.  
  32. # Python 3.8 #########################
  33.  
  34. while cmd := input("Enter a command, or just press enter to quit: \n> "):
  35.    
  36.     if match := re.match(r"Alexa, play ((\w+\s)*\w+)\.", cmd):
  37.         song_name = match.group(1)
  38.         print(f"Playing {song_name}...")
  39.        
  40.     elif match := re.match(r"Alexa, dim the lights in ((\w+\s)*\w+)\.", cmd):
  41.         room_name = match.group(1)
  42.         print(f"Dimming the lights in {room_name}...")
  43.        
  44.     elif match := re.match(r"Alexa, call ((\w+\s)*\w+)\.", cmd):
  45.         name = match.group(1)
  46.         print(f"Calling {name}...")
  47.        
  48.     else:
  49.         print(f"I don't understand.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement