2017-02-11 36 views
2

試圖做一個布爾測試循環,其中用戶需要輸入10到1000之間的數字,包括10和1000,他們需要留在這循環,直到他們輸入正確的數字,然後我會完成else語句。我想返回一個「二重奏」列表中的頂級元素的數量

我嘗試這樣做:

while (num_years < 10) and (num_years > 1000): # boolean test , note == (= will give you an error!) 
    print("Your input must be 10 and 1000") 
    input("Enter number of years to simulate (currently " + str(num_years) + "): ") 
else: 
    print() 

這:

while (num_years == range(10, 1000)): # boolean test , note == (= will give you an error!) 
    print("Your input must be 10 and 1000") 
    input("Enter number of years to simulate (currently " + str(num_years) + "): ") 
else: 
    print() 
+1

OFFTOPIC評論:從中我記得你永遠不會將你的問題標記爲答案,請參閱[this](http://stackoverflow.com/help/someone-answers)鏈接。 – MaLiN2223

+1

提示*「您的輸入必須是10和1000」*沒有意義(一個數字不能(http://www.dictionary.com/browse/be)有兩個不同的數字......此外,即使您添加**之間**這個詞在適當的位置上,如果你想要**包含**,(10個是允許的1000個)或**獨佔**,(10個是1000個是不允許的),你仍然應該澄清。如果你在源自他們課程的python問題標記爲你的python問題的答案,那麼你在python的作者上的在線課程將會很感激,因爲他們中的大多數是(http://stackoverflow.com/users/7443013/supernova):) – shash678

+0

between幷包括 – Supernova

回答

0

好吧,首先:

(num_years < 10) and (num_years > 1000) 

這始終是假,因爲一個數不能少於10同時超過1000個。

的所有

num_years == range(10, 1000) 

二行不通,因爲你必須在右邊的左邊和generator(可能)的整數。

但是你可以嘗試這樣的事情:

possible_years = range(10,101) 
num_years = 50 
while num_years in possible_years: # or 9 < num_years < 100 
    print("Your input must be 10 and 1000") 
    entered_years = input("Enter number of years to simulate (currently " + str(num_years) + "): ") 
    num_years = int(entered_years) # remeber to check if entered_years is int before this line! 
else: 
    print() 

但是仍有追趕,你需要檢查,如果用戶輸入一個整數。

+0

它沒有工作,我把它放在10,它通過循環, – Supernova

+0

@Supernova因爲你排除了1 0,範圍爲'num_years <10'。我編輯了我的答案以滿足您的需求。 – MaLiN2223

+1

'list(range(...))'不是必須的'range()'支持'in'運算符並且更具有內存效率。 50範圍內(10,1001)== True。注意:範圍是'range(10,1001)': – AChampion

0

你永遠不會改變num_years的價值,所以條件永遠不會改變。修復您的布爾邏輯:

num_years = 0 
while (num_years < 10) or (num_years > 1000): 
    print("Your input must be 10 and 1000") 
    num_years = int(input("Enter number of years to simulate (currently {}): ".format(num_years))) 
else: 
    print() 

然而,這種假設有人正在進入一個號碼,如果你需要覆蓋一個非數字值,則使用try: except: pass塊:

try: 
    num_years = int(input("Enter number of years to simulate (currently {}): ".format(num_years))) 
except ValueError: 
    pass 

注:=是分配不平等測試。使用in來測試值是否在listrange中。

0

你需要做的是一個while循環來檢查輸入是否有效。如果不是,它會再次循環。你也從來沒有把輸入分配給一個變量。

n = None 
while n is None or not 10 <= n <= 1000: 

    if n is not None: # Only happens if it's not the first input 
     print('Invalid number, please try again') 

    try: 
     n = int(input('Enter number of years to simulate (10-1000): ')) # Convert the 
                      # string to an int 
    except: 
     pass 

print('Simulating {} years...'.format(n)) 

它將繼續說一次模擬一個有效的數字輸入,因爲

not 10 <= n <= 1000 

將評估爲false。

您可以在此鏈接試試:https://repl.it/FftK/0

+0

當我輸入範圍內的數字例如15它仍然說輸入10到1000之間的數字 – Supernova

+0

你在什麼Python版本?我在3.6,並按預期工作。看看repl鏈接,它在那裏工作。 –

+0

幾乎都有它,它會檢查拳頭時間,但不是第二個: – Supernova

1

你可以嘗試這樣的事情,因爲它如果用戶輸入一個有效的數字還檢查:

while True: 
    number = input("Enter number of years to simulate (10-1000): ") 
    try: 
    years = int(number) 
    if years >= 10 and years <= 1000: 
     break 
    else: 
     print("Your input must be between 10 and 1000 inclusive") 
    except ValueError: 
    print("That's not an int!") 

print("Simulating %d years..." % years) 

實例應用:

Enter number of years to simulate (10-1000): asd 
That's not an int! 
Enter number of years to simulate (10-1000): 5 
Your input must be between 10 and 1000 inclusive 
Enter number of years to simulate (10-1000): 245 
Simulating 245 years... 

試試吧here!

+1

這似乎是最好的答案(不是問題存在,而是評論)。 +1爲你先生,因爲我從來不會猜到這是OP要求的。 – MaLiN2223

0

N =無 而n是無或不10 < = N < = 1000:

if n is not None: # Only happens if it's not the first input 
    print('Invalid number, please try again') 

try: 
    n = int(input('Enter number of years to simulate (10-1000): ')) # Convert the 
                     # string to an int 
except: 
    pass 

打印( '模擬{}年......' 制(n))。

相關問題