Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
137 views
in Technique[技术] by (71.8m points)

python - How do I change the code to whatever I input?

So I've been working on this code and I'm trying to make it so that what I write will change the code's result.

import phonenumbers
from phonenumbers import geocoder
from phonenumbers import carrier
phone_number1 = phonenumbers.parse("+12123456789")
print(phone_number1)
print(geocoder.description_for_number(phone_number1,'en'))
print(carrier.name_for_number(phone_number1,'en'))
phone_number2 = phonenumbers.parse("+12123456789")
print(geocoder.description_for_number(phone_number2,'en'))

The result I get depends on what I write in the phonenumber.parse() but I can't figure out how to make it what I input change that phonenumber.parse(), example:

What is your phone number?

+12123456789

and that makes the phonenumber.parse() become phonenumber.parse("+12123456789")

There's probably a very simple answer to this which I'm just not seeing...

This is most likely very poorly explained so any type of help would be useful :)

question from:https://stackoverflow.com/questions/65938773/how-do-i-change-the-code-to-whatever-i-input

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use an f string literal. This will format the string passed into the parser with the value of the user_In variable. Here is the code:

user_In = input("What is your phone number? 
")
number1 = phonenumbers.parse(f"{user_In}")

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...