如何比较字符串python中的特定项目

时间:2018-11-26 03:04:42

标签: python

例如在给定的输入中,第四位必须比第五位大一位

input = "5000-0000-0000"    
if input[3] != input[5] + 1    
    return false 

1 个答案:

答案 0 :(得分:1)

如果您考虑import datetime import requests import sys import json class APIError(Exception): """ APIError represents an error returned by the OpenRates API. """ pass class Currency(object): """ Currency object will store all currency related data such as present rates and historical rates """ # def __init__(self, today_rate, yesterday_rate, lastweek_rate): def __init__(self, exchange_rates): # Store the raw data from the exchange rate API for today's rates - this will be used in the average rate calculations self.currency_data = exchange_rates class Time(object): """ Here are all the time related elements we will need for this program to work """ def __init__(self): # Call the API here for use in the Currency object later self.url = "http://api.openrates.io/" def call_api(self, **kwargs): """ Openrates API takes in Base Currency, Dates and Destination Currency as parameters, individually, however, not altogther """ # Call the API by requesting the URL. Use `json()` to decode the raw JSON data. response_data = requests.get(self.url, kwargs).json() # Check for an error and throw an exception if needed. if "Error" in response_data: raise APIError(response_data["Error"]) # Return the decoded data. return response_data def get_today(self, base_curr): # Call today's exchange rate API # date = datetime.date.today( # today_rate = self.call_api(str(date), base=base_curr) # today_rate = self.call_api(str(date)) self.url = "http://api.openrates.io/latest" today_rate = self.call_api(base=base_curr) return Currency(today_rate) def get_yesterday(self, base_curr): # Call today's exchange rate API date = datetime.date.today() - datetime.timedelta(days = 1) self.url = "http://api.openrates.io/" + str(date) yesterday_rate = self.call_api(base=base_curr) # print(yesterday_rate) #yesterday_rate = self.call_api(str(date), return Currency(yesterday_rate) def get_last_week(self, base_curr): #Return last week's datetime date = datetime.date.today() - datetime.timedelta(days = 7) self.url = "http://api.openrates.io/" + str(date) lastweek_rate = self.call_api(base=base_curr) return Currency(lastweek_rate) def home(): # Get the conversion amount from the website. conv_amt = input("conv_amt") print("Amount to change is:", conv_amt) base = input("base_curr") print("Changing from:", base) dest = input("dest_curr") print("Changing to:", dest) #Create Time client time = Time() if base: try: pony = time.get_today(base) today_amt = conv_amt * pony.currency_data["rates"][dest] except APIError: pony = "(error)" yesterpony = time.get_yesterday(base) yesterday_amt = conv_amt * yesterpony.currency_data["rates"][dest] lastweekpony = time.get_last_week(base) lastweek_amt = conv_amt * lastweekpony.currency_data["rates"][dest] return today_amt, yesterday_amt, lastweek_amt if __name__ == "__main__": home() input[3]是什么,您很快就会意识到它们是字符,而不是可以加或与数学运算相比较的数字(想想如果写了input[5]

您可以使用input = "Andrew Francis"看到它。

幸运的是,如果您的字符串仅包含组成有效数字的字符,则可以使用print(type(input[3]))函数将其转换为(例如)整数。因此,尝试int()看看会得到什么。