2015-06-20 53 views
0

此代碼將檢查一個字符串並按順序返回數字,並將提供單個數字,因爲它們出現在字符串中。我知道有很多方法比我的代碼更好地查找字符串中的數字,這是一項任務。我只有循環的問題,循環應該運行直到字符串結束並終止。我無法做到這一點。我正在編寫一個查找字符串中的數字的程序

#variables 
start = None 
end = None 
zero= None 
one = None 
two = None 
three = None 
four = None 
five = None 
six = None 
seven = None 
eight = None 
nine = None 
check = None 
def numfind(): 
    #will find numbers in a string and allocate position in the string 
    global zero 
    global one 
    global two 
    global three 
    global four 
    global five 
    global six 
    global seven 
    global eight 
    global nine 
    global end 
    global start 
    zero=text.find('0',end) 
    one=text.find('1',end) 
    two=text.find('2',end) 
    three=text.find('3',end) 
    four=text.find('4',end) 
    five=text.find('5',end) 
    six=text.find('6',end) 
    seven=text.find('7',end) 
    eight=text.find('8',end) 
    nine=text.find('9',end) 
def numstart(): 
    #will find the smallest number from among the previous function output and will allocate it as the start of the number, in "start" variable, will use slicing function to return the number 
    global zero 
    global one 
    global two 
    global three 
    global four 
    global five 
    global six 
    global seven 
    global eight 
    global nine 
    global start 
    for n in [zero,one,two,three,four,five,six,seven,eight,nine]: 
     if start is None: 
      if n != -1: 
       start = n 
       #print start 
      else: 
       continue 
     else: 
      if n != -1: 
       if start>n: 
        start=n 
        #print start 
      else: 
       continue 
def numend1(): 
    #Will find the space after numbers begin, will use "end" in the slicing function to return the first number from the string. 
    global start 
    global end 
    end=text.find(" ",start) 
    #print end 
def endchecker(): 
    #this is some bullshit I came up with to terminate the loop, but doesn't work :(
    global zero 
    global one 
    global two 
    global three 
    global four 
    global five 
    global six 
    global seven 
    global eight 
    global nine 
    global start 
    global end 
    global check 
    for n in [zero,one,two,three,four,five,six,seven,eight,nine]: 
     if check is None: 
      if n != -1: 
       check = n 
       #print check 
      else: 
       check=n 
     else: 
      if n != -1: 
       check=n 
        #print check 
      else: 
       if check>n: 
        continue 
       else: 
        check = n 
text=raw_input("Please enter a string containing a set of numbers:") 
while True: 
    numfind() 
    endchecker() 
    print check 
    if check == -1: 
     break 
    numstart() 
    numend1() 
    try: 
     if end!=-1: 
      number1=float(text[start:end]) 
      print number1 
     else: 
      number1=float(text[start:]) 
      print number1 
      break 
    except: 
     print "Error" 
     break 
    print "End of Program" 
+0

你的代碼中的所有全局變量正在發生什麼? –

+5

不使用全局變量,使用返回值;使用列表。 – Daniel

+0

您在代碼中大量使用了'globals' ......這並不是建議的 –

回答

0

我檢查你的代碼,但你會做什麼有點困惑,看來你是想從一個文本數子。

#python 
text=raw_input("Please enter a string containing a set of numbers:") 

s = "" 
for c in text: 
    if c >= '0' and c <= '9': 
     s += c 
    else: 
     if len(s) != 0: 
      print s 
      s = "" 
if len(s) != 0: 
    print s 

print "End of Program" 

希望能幫助你一些。

一些建議,編碼時更好的調試,不寫大塊的代碼,但從來沒有試圖運行它,一個小而有用的代碼更好。

順便說一句,避免使用Global會好得多,這會讓你的代碼難以閱讀,函數參數和返回值會更好。

0

男人,你的代碼是怎麼回事?我不完全確定你想要用你的代碼做什麼。如果您只需檢查一個字符串是否包含數字並按順序顯示這些數字,那麼您可以使用正則表達式輕鬆完成這些操作。

#python 
import re 
string = raw_input("Enter a string: ") 

match = re.findall(r'\d', string) 

# print the found numbers in the same sequence 
# as found in the string 
for i in match: 
    print i, 

如果要按順序打印數字,可以使用sorted()函數。如下修改for循環。

match = re.findall(r'\d+', string) 

還是非貪婪版本:

for i in sorted(match): 
    print i, 

如果您正在尋找長串,你可以按如下修改模式

match = re.findall(r'\d+?', string) 

同樣,我不完全確定這是否解決了你的問題。

相關問題