2015-11-06 57 views
-1

我工作的Python中的項目,有以下問題:如何在一個字符串搜索整數一個單詞的鄰近

我有一個搜索功能查找特定的詞,我希望能夠發現一個整數之內,比如給定單詞的5-10個字符(向左)。

在我的代碼中,n(小時)乘以熱量計數。我已經設置了輸入,以便當一個人輸入他們的每日時間表時,他們必須輸入如I did 6 walking; 6是小時數,步行對應於特定的卡路里量。空的if聲明是我想要實現此整數查找器的位置。

下面是代碼:

x = 0 
y = 12 
### N = Hours for given activity ### 
### X = Total calories for 12 hour period ### 
### Y = Total amount of hours 'used' by description ### 
### E = Input text ### 

print("READ DIRECTIONS, IMPORTANT") 
print("Describe your schedule only using the following strings: walking, " + 
     "sitting, standing, running, swimming, exercising, eating. We will use " + 
     "hour as a time stamp for our process of which we calculate your daily " + 
     "calories. Please do not use any strings outside of the strings we gave " + 
     "you. Make sure you only describe your daily activities within a 12 " + 
     "hour time-frame, and accurately describe the time-frame of your " + 
     "activities using integers. If it is varied, try thinking of an average " + 
     "amount of hours you do an activity. AND MAKE SURE ALL STRINGS ARE " + 
     "LOWERCASE, if you do not enter the correct string or enter it " + 
     "incorrectly, the search function will ignore that string. Also, DO NOT " + 
     "type 'I did thing for x hours.' Simply put, 'I did 6 walking.' " 
     "The search function will automatically read that number as an hour. " + 
     "If you do talk like a normal human being, the program will not "+ 
     "like you and refuse to work. DO NOT use multiple of something, " + 
     "I did 6 walking. And then later you say, I did 3 walking. " + 
     "Sum it up into one string please." 
print("Example: After so and so, I did 6 walking. Remember, this is the total " + 
     "amount walked in time-frame. Not, I did 3 walking ... " + 
     "I did more 3 walking." 
data = input("Enter your description: "): 
    p = 1 
    e = data 
    def(e) search_func(walking, sitting, standing, running, swimming, exercising, eating): 
     if walking: 
      if 
       if n > 0: 
        x - 272*n 
        y - n 
    if sitting: 
     if 
      if n > 0: 
       y - n 
    if standing: 
     if 
      if n > 0: 
       x - 50*n 
       y - n 
    if swimming: 
     if 
      if n > 0: 
       x - 590*n 
       y - n 
    if running: 
     if 
      if n > 0: 
       x - 398*n 
       y - n 
    if exercising: 
     if 
      if n > 0: 
       x - 420*n 
       y - n 

    if not (hour, walking, sitting, standing, running, swimming, exercising, eating): 
     print"Did not use any proper strings. Please enter your information correctly." 

if y < 0: 
    print"Please fit your schedule within a 12 hour time frame" 

print "Calories added in time frame: %x" % x 

if x < 0: 
    print "Calories burned in time frame: %x" % x 

我們的目標是太使程序比可以計算基於一個人的飲食和鍛鍊計劃,在世界任何地方安排。當然,我們正在上高中,所以我們正在採用上面的方法。

在一個完美的世界,該計劃將能夠:

  • 梳理一下一個人每天的時間表是基於文本的輸入
  • 能讀懂他們的卡路里攝入量
  • 計算一下他們應該吃的食物
  • 使用當地的地區的食物
  • 瞭解不同的疾病人應該注意的

回答

0

首先,你必須知道,在Python中,字符串是考慮某種列表!那麼試試這個:

int(re.search(r'\d+', yourstring).group()) 

如果在字符串嘗試多個整數:

map(int, re.findall(r'\d+', yourstring)) 
相關問題