简体   繁体   中英

How to create a program that takes user input USA time and convertes it in BG time with python?

Given the difference in hours between USA and Bulgaria = 7h I have this list with hours. The idea is, for example, the user gives input 12 AM, and the program loops thru the list find what the input 12 AM (USA time) equals and returns the answer 7:00(BG time) and vice versa.

usa_time = ['12AM=7:00','1AM=8:00','2AM=9:00','3AM=10:00','4AM=11:00','5AM=12:00','6AM=13:00','7AM=14:00','8AM=15:00','9AM=16:00','10AM=17:00','11AM=18:00','12PM=19:00','1PM=20:00','2PM=21:00','3PM=22:00','4PM=23:00','5PM=00:00','6PM=1:00','7PM=2:00','8PM=3:00','9PM=4:00','10PM=5:00','11PM=6:00']

user_input = input("Enter desired hour for coversion:")

It would be much better to use dictionaries in your case. For example:

usa_time = { '24:00' : 7,
             '01:00' : 8,
             '02:00' : 9,
             .... etc 
}

user_input = input("Enter desired hour for coversion:")

usa_time[user_input]

Given the input matches one of the keys in your dictionary it will return the desired time. Please note this is not a good solution nor approach to your problem. I just showed you an improvement to your solution. There are more efficient solutions involving libraries pytz and timezone. You can see an example here: How to convert local time string to UTC?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM