Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Beginner Python: is there a cleaner way to to rewrite my current code? I am quite new to Python. Here I have written a code
Beginner Python: is there a "cleaner" way to to rewrite my current code?
I am quite new to Python. Here I have written a code that translates alpha characters to numerics in a phone number. My code will just assume that the format of the phone number is entered correctly.
def phone(phone_number): converted_phone_number = "" for number in phone_number: if number in "ABC": converted_phone_number += '2' elif number in "DEF": converted_phone_number += '3' elif number in "GHI": converted_phone_number += '4' elif number in "JKL": converted_phone_number += '5' elif number in "MNO": converted_phone_number += '6' elif number in "PQR": converted_phone_number += '7' elif number in "TUV": converted_phone_number += '8' elif number in "WXYZ": converted_phone_number += '9' else: converted_phone_number += number return converted_phone_number def main(): print(phone("604-GOT-JUNK")) if __name__ == "__main__": main()
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started