是否具有将变量“自动完成”到所需库的功能?

时间:2019-01-18 11:21:41

标签: python

我正在尝试将一个变量设置为库中的变量。有命令执行此操作吗?

我正在尝试制作一个简单的时区转换器,我想检查输入变量,但是我只能从pytz中检查列表中的变量,所以我想“自动完成”变量。我可以这样做吗?

import time
import pytz
country = input("enter country")

from datetime import datetime
from pytz import timezone

fmt = "%H:%M %p"

now_utc = datetime.now(timezone('UTC'))
print (now_utc.strftime(fmt))

from pytz import all_timezones
if country in all_timezones:
    country = #completed country in list 'all_timezones'
    timecountry = now_utc.astimezone(timezone(country))
    print (timecountry.strftime(fmt))

1 个答案:

答案 0 :(得分:1)

因此,您正在寻找一种将用户输入与public Transform sightStart, sightEnd, sightStart2, sightEnd2; public Vector3 direction = Vector3.right; public Vector3 direction2 = Vector3.right; public float speed = 2f; public bool spotted = false; public bool rotate, moveEnemy = false; public Camera mainCam; public GameObject EndSight, EndSight2; void Start () { speed = 2f; spotted = false; rotate = false; moveEnemy = false; mainCam = GameObject.Find ("Main Camera").GetComponent<Camera> (); } void Update () { Behaviours (); if (mainCam.WorldToScreenPoint (this.transform.position).x > Screen.width / 2) { direction.x = -1; InvokeRepeating ("EnemySight", 0, 10f); if (moveEnemy == false) { RayCasting (); Destroy (gameObject, 7f); } } else { direction.x = 1; InvokeRepeating ("EnemySight", 0, 10f); if (moveEnemy == false) { RayCasting2 (); Destroy (gameObject, 7f); } } } void EnemySight() { if (rotate == false) { if (mainCam.WorldToScreenPoint (this.transform.position).x > Screen.width / 2) { EndSight.transform.Translate (direction2 * speed * Time.deltaTime); direction2.x = 0; direction2.y = 1; StartCoroutine (wait1 ()); } else { EndSight2.transform.Translate (direction2 * speed * Time.deltaTime); direction2.x = 0; direction2.y = 1; StartCoroutine (wait1 ()); } } else if (rotate == true) { if (mainCam.WorldToScreenPoint (this.transform.position).x > Screen.width / 2) { EndSight.transform.Translate (direction2 * speed * Time.deltaTime); direction2.x = 0; direction2.y = -1; StartCoroutine (wait2 ()); } else { EndSight2.transform.Translate (direction2 * speed * Time.deltaTime); direction2.x = 0; direction2.y = -1; StartCoroutine (wait2 ()); } } } IEnumerator wait1() { yield return new WaitForSeconds (0.7f); rotate = true; } IEnumerator wait2() { yield return new WaitForSeconds (0.7f); rotate = false; } void RayCasting() { Debug.DrawLine (sightStart.position, sightEnd.position, Color.black); spotted = Physics2D.Linecast (sightStart.position, sightEnd.position, 1 << LayerMask.NameToLayer("Items")); } void RayCasting2() { Debug.DrawLine (sightStart2.position, sightEnd2.position, Color.black); spotted = Physics2D.Linecast (sightStart2.position, sightEnd2.position, 1 << LayerMask.NameToLayer("Items")); } void Behaviours() { if (spotted == true) { transform.Translate (direction * speed * Time.deltaTime); speed = 2f; } else if (spotted == false) { speed = 0; } } void OnTriggerExit2D (Collider2D col) { if (col.tag == "object1") { speed = 0; CancelInvoke(); } if (col.tag == "object2") { speed = 0; CancelInvoke(); } if (col.tag == "object3") { speed = 0; CancelInvoke(); } if (col.tag == "object4") { speed = 0; CancelInvoke(); } } } 中的字符串进行匹配并寻找有效时区的方法。

据我所知,没有内置函数可以执行此操作,您必须自己完成操作。

这不是一项紧迫的任务,因为您可能有多个选择(假设用户仅输入“欧洲”),则必须考虑到这一点

可能的方法如下:

all_timezones

通过列表理解,我在import datetime import time import pytz country = input("Contry name: ") now_utc = datetime.datetime.now(pytz.timezone('UTC')) fmt = "%H:%M %p" while True: possible_countries = [ac for ac in pytz.all_timezones if country in ac] if len(possible_countries) == 1: cc = possible_countries[0] timecountry = now_utc.astimezone(pytz.timezone(cc)) print(timecountry.strftime(fmt)) break elif len(possible_countries) > 1: print("Multiple countries are possible, please rewrite the country name") for cs in possible_countries: print(cs) country = input("Contry name: ") else: print("No idea of the country, here are the possible choices") for cs in pytz.all_timezones: print(cs) country = input("Contry name: ") 中查找包含用户输入的所有字符串。如果只有一个,脚本将假定它是正确的脚本并执行任务。否则,如果有多个可能性,它将打印它们(每行一个for循环,但是您可以只打印列表,使其在屏幕上更短),然后要求用户重写国家/地区名称。如果不匹配,则仅打印所有可能性。您可能会发现在命令行上看到它很丑陋,但是您应该先了解一下然后再加以改进。

如果您还希望检查用户输入中的拼写错误,则要困难得多。