2013-01-01 53 views
1

我想將這個while循環應用到下面的for循環中。 我曾嘗試在if語句之前放置while循環,在每個if語句中。 當我把它放在if語句之前(在for循環中)時,它會詢問用戶一次,然後返回整個範圍(1,8)的相同輸入。 我希望這while循環適用於每個問題,七個項目2至8 我該如何實現它。誰能幫助請,謝謝while循環插入一個while循環到for循環中

def prices(): 
    x = range (1,8) 
    item=2 
    price=0 
    for item in x: 
     item +=1 
     print "\n\t\titem",item 
     price = int(raw_input("Enter your price : ")) 
     if price <10: 
      price=1 
      print "This is ok" 


     if price >9 and price <45: 
      price +=5 
      print "This is great" 


     if price >44 and price <70: 
      price +=15 
      print "This is really great" 

     if price >69: 
      price +=40 
      print "This is more than i expected" 

    print "\nYou now have spent a total of ",price 

prices()  

def valid_entry(): 
    price = 110 
    invalid_input = 1 
    while price< 0 or price> 100: 
     if invalid_input >=2: 
      print "This is an invalid entry" 
      print "Please enter a number between 0 and 100" 
     try: 
      price= int(raw_input("Please enter your price : ")) 
     except ValueError: 
      price = -1 
     invalid_input +=1 

末是缺乏響應的跡象,表明它是一個愚蠢的問題或者是沒可能做什麼?

這是否使它更清晰?

def prices(): 
    x = range (1,8) 
    item=2 
    price=0 
    for item in x: 
     item +=1 
     print "\n\t\titem",item 
     valid_entry()#should it go here 
     price = int(raw_input("Enter your price : ")) 
     valid_entry()#should it go here 
     if price <10: 
      valid_entry()#should it go here and so on for the following 3 if conditions 
      price=1 
      print "This is ok" 


     if price >9 and price <45: 
      price +=5 
      print "This is great" 


     if price >44 and price <70: 
      price +=15 
      print "This is really great" 

     if price >69: 
      price +=40 
      print "This is more than i expected" 

    print "\nYou now have spent a total of ",price 
+0

不好意思,你會介意將你的問題改爲可以理解的事情? – Rubens

+0

當然魯本斯,你不明白什麼部分 – miguel

+0

基本上,如果用戶輸入的東西除了0到100之間,while循環中的錯誤消息將顯示告訴他們它的一個無效輸入並從o輸入數字到100 .for for循環的七次運行將會運行 – miguel

回答

1

你可以嘗試這樣的事情(道歉,如果這不是你要找的)。很高興解釋任何沒有意義的東西 - 總體思路是,它循環播放8個項目的範圍,每次都要求有效的價格,並繼續詢問輸入的值是否不是指定範圍內的數字。因爲這可能是一個任務,我試圖保持它作爲共進退你已經證明了你知道的概念(這裏唯一的例外可能是continue):

def valid_entry(): 
    # Here we define a number of attempts (which is what I think 
    # you were doing with invalid_input). If the person enters 10 
    # invalid attempts, the return value is None. We then check to 
    # see if the value we get back from our function is None, and if 
    # not, proceed as expected. 
    num_attempts = 0 
    while num_attempts < 10: 
     # Here we do the input piece. Note that if we hit a ValueError, 
     # the 'continue' statement skips the rest of the code and goes 
     # back to the beginning of the while loop, which will prompt 
     # again for the price. 
     try: 
      price = int(raw_input("Enter your price : ")) 
     except ValueError: 
      print 'Please enter a number.' 
      num_attempts += 1 
      continue 

     # Now check the price, and if it isn't in our range, increment 
     # our attempts and go back to the beginning of the loop. 
     if price < 0 or price > 100: 
      print "This is an invalid entry" 
      print "Please enter a number between 0 and 100" 
      num_attempts += 1 
     else: 
      # If we get here, we know that the price is both a number 
      # and within our target range. Therefore we can safely return 
      # this number. 
      return price 

    # If we get here, we have exhausted our number of attempts and we will 
    # therefore return 'None' (and alert the user this is happening). 
    print 'Too many invalid attempts. Moving to the next item.' 
    return None 


def prices(): 
    # Here we go from 1-8 (since the upper bound is not included when 
    # using range). 
    x = range(1,9) 

    # Here we use a variable (total) to store our running total, which will 
    # then be presented at the end. 
    total = 0 

    # Now we iterate through our range. 
    for item in x: 
     print "\n\t\titem",item 

     # Here is the important part - we call our valid_entry function and 
     # store the result in the 'price' variable. If the price is not 
     # None (which as we saw is the return value if the number of attempts 
     # is > 10), we proceed as usual. 
     price = valid_entry() 
     if price is not None: 
      if price <10: 
       # Note this should probably be += 1 
       total += 1 
       print "This is ok" 

      elif price >= 10 and price < 45: 
       total += 5 
       print "This is great" 

      elif price >= 45 and price < 70: 
       total += 15 
       print "This is really great" 

      # We know that price >= 70 here 
      else: 
       total += 40 
       print "This is more than i expected" 

    # Now print out the total amount spent 
    print "\nYou now have spent a total of ", total 

prices() 
+0

嗨RocketDonkey,我不介意多少次用戶輸入錯誤的數據,但它是有道理的,他們只有一定的時間輸入錯誤的數量。事情是,我希望while循環首先讓用戶單獨輸入每個項目的價格,2到8.當我把while循環放在for循環中的第一個if語句之前時,它將第一個輸入賦給item 2並自動爲其餘項目提供相同的輸入。感謝您的代碼,我會嘗試一下。 – miguel

+0

@miguel我相信我遵循,但聽起來不錯 - 看看上面的代碼至少是你想要的/期望的,如果是的話,我很樂意回答任何其他問題(或與你的工作,讓它的功能如你所期望的那樣)。新年快樂:) – RocketDonkey

+0

謝謝生病讓你知道它是否在5分鐘內有效,祝你新年快樂 – miguel