2014-04-01 32 views
2

我試圖獲取用戶輸入被分成2個整數的列表2號的一組數字之間,但不幸的是我似乎無法讓他們變成整數,但我也不確定我的支票是否會正常終止並返還我所需的支票。得到原始輸入是與一些檢查

def getlohiint(): 
    lohilist = [] 
    lohi = raw_input("Enter min and max number of points to use(e.g., 2 1000): ") 
    lohilist = lohi.split() 
    for x in lohilist: 
     int(x) 
    if lohilist[0]<=2 and lohilist[1]<=1000 and lohilist[0]<lohilist[1]: 
     break 
    else: 
     prompt = "%d is not in the range of 2 to 1000; " 
     prompt1 = "must be at least 2" 
     prompt2 = "must be no higher than 1000" 
     if lohilist[0]<2: 
      print prompt + prompt1 
     elif lohilist[1]>1000: 
      print prompt + prompt2 
     else: 
      print prompt + prompt1 + prompt2 
     lohi 
    high = lohilist[1] 
    low = lohilist[0] 
    return(low, high) 
+0

但畢竟我是添加L- ohilist = [int(x)for lohi.split()]我不確定如何正確執行檢查。特別是第一個if循環 –

+1

您是不是指第一個if語句的塊?一個'if'塊不是一個循環,它只執行一次。如果你剛進入tutorialspoint.com,發現'break'出現在'if'語句之後,那是因爲它們在這些例子中的'for'或'while'循環中。如果你不想在if語句中做任何事情,使用'pass',它本質上是一個'nop',或者沒有操作語句(它什麼也不做)。你也可以簡單地將整個if語句放在圓括號中,並在它前面加一個'not',並將其作爲錯誤情況;從而將「if ... else」更改爲「if」語句。 – Matthew

回答

6

永遠不會將int(x)的結果賦值給任何東西。最簡單的方法與列表理解來實現:

lohilist = [int(x) for x in lohi.split()] 

請注意,您可以一次分配給多個目標:

low, high = [int(x) for x in lohi.split()] 

將在lohi一切轉換爲整數,並分配給兩個變量中一氣呵成。

您可能想測試例外太這裏:

def getlohiint(): 
    while True: 
     lohi = raw_input("Enter min and max number of points to use(e.g., 2 1000): ") 
     try: 
      low, high = [int(x) for x in lohi.split()] 
     except ValueError: 
      # either not integers or not exactly 2 numbers entered. 
      print "You must enter exactly 2 numbers here" 
      continue 

     if low <= 2 and high <= 1000 and low < high: 
      # valid input, return 
      return low, high 

     if low > 2: 
      print 'The minimum must be 2 or lower' 
     if high > 1000: 
      print 'The maximum must be 1000 or lower' 
     if low >= high: 
      print 'The maximum must be greater than the minimum' 
+0

我總是忘記這種方法......(讓我感到很蠢)謝謝,但我該如何正確地進行檢查。我不認爲中斷是合適的方法 –

+0

我有這樣的最初減去嘗試和除了,但我仍然有問題獲取和整數。爲什麼會有嘗試聲明?非常感謝 –

+0

如果字符串包含任何不能整合到一個整數的內容,那麼'int()'函數會引發'ValueError',如果右邊的列表理解導致更少或更多比2個值。 'try'語句處理這兩種情況。 –

3

int(x)返回一個整數值,它不修改x的值。

你可能想是這樣的:

def getlohiint(): 
    lohilist = [] 
    lohi = raw_input("Enter min and max number of points to use(e.g., 2 1000): ") 
    lohilist = lohi.split() 
    lohilist = [int(x) for x in lohilist] 
1

我會摘要了一點:作出這樣的分析從輸入整數列表的功能,那麼另一個函數採用整數和測試的名單,他們適合自己的標準:

import sys 

# version compatibility shim 
if sys.hexversion < 0x3000000: 
    # Python 2.x 
    inp = raw_input 
else: 
    # Python 3.x 
    inp = input 

def get_ints(prompt, delimiter=None): 
    while True: 
     try: 
      return [int(i) for i in inp(prompt).split(delimiter)] 
     except ValueError: 
      pass 

def get_lo_hi_ints(prompt, min_=None, max_=None): 
    while True: 
     try: 
      lo, hi = get_ints(prompt) 

      if lo >= hi: 
       print("First value must be < second value") 
      elif (min_ is not None and lo < min_): 
       print("Low value must be >= {}".format(min_)) 
      elif (max_ is not None and hi > max_): 
       print("High value must be <= {}".format(max_)) 
      else: 
       return lo, hi 
     except ValueError: 
      print("Please enter 2 values!") 

那麼你可以使用它像

min_points, max_points = get_lo_hi_ints("Enter min and max number of points to use(e.g., 2 1000): ", 2, 1000) 
+0

而不是版本測試,只需嘗試引用'raw_input';如果你得到一個'NameError',你可以用'input'來代替。 –