2013-10-30 19 views
1

所以,我基本上試圖使它不會繼續使用函數,除非在輸入後輸入yes/no。這個功能只會將你的輸入添加到列表中。基本上我希望程序能讓你輸入一堆不同的數字,然後在輸入結束時詢問你是否要繼續。如果你按是的,我希望它繼續進行功能,但在我的代碼中,我做到了,所以每個輸入都在一個新的輸入行上,而不是在同一個輸入行中,所以當我將它追加到列表中時,我正在使用一段時間聲明。隨着循環的Python驗證

如果您需要我澄清更多,請讓我知道。

代碼:

next2=input("How many would you like to add? ") 
print("") 
count = 0 
while count < int(next2): 
    count = count + 1 
    next3=input(str(count) + ". Input: ") 
    add(next3) 
print("") 
check=input("Are you sure? (Y/N) ") 
while check not in ("YyYesNnNo"): 
    check=input("Are you sure? (Y/N) ") 
if check in ("YyYes"): 
    home() 

功能:

def add(next2): 
    numbers.append(next2) 
    sort(numbers) 

當你運行這個程序應該是這樣的:

How many numbers would you like to add? "4" 
1. Input: 4 
2. Input: 3 
3. Input: 2 
4. Input: 1 

Are you sure? (Y/N): Y 

> append the inputs here 

如果他們點擊不,這使他們的我已經安裝了該程序的主屏幕。

這就是它現在所做的:

您想要添加多少個數字? 「4」 1.輸入: 「4」

追加到列表 2.輸入: 「3」 追加到列表 3.輸入: 「2」 追加 列出 4.輸入:「 1「 追加到清單 確定嗎? (Y/N):「Y」 排序列表顯示

+1

我不清楚你確切的問題是什麼。似乎你想追加數字,如果檢查結果是肯定的,或者打電話回家就是檢查是不是?期望的結果是什麼,目前的結果是什麼,它們有什麼不同? – hankd

+0

對不起,我現在要添加它的功能。我添加了額外的信息。 – user2812028

回答

2

據其附加到列表中,當你進入他們(你問之前,如果他們知道),因爲你所呼叫的循環內的附加功能。您想要將它們存儲在某個臨時結構中,並且只有在檢查確定後才能添加它們。

next2=input("How many would you like to add? ") 
print("") 
count = 0 
inputs = [] 
while count < int(next2): 
    count = count + 1 
    next3=input(str(count) + ". Input: ") 
    inputs += [next3] 
print("") 
check=input("Are you sure? (Y/N) ") 
while check not in ("YyYesNnNo"): 
    check=input("Are you sure? (Y/N) ") 
if check in ("YyYes"): 
    for userInput in inputs: 
     add(userInput) 
else: 
    home() 
+0

謝謝這麼多!有用! – user2812028