2013-09-29 73 views
-1

再一次,我仍然得到蟒蛇的坑。我製作了一個程序,讓用戶猜測一個由計算機隨機生成的「幻數」。經過5次不正確的嘗試後,它使用兩個隨機生成的變量(其總和等於幻數)提供了附加問題形式的提示。我該如何改善這個循環?

這是代碼:

def moot(): 
    count = 0 
    # magicNumber is the randomly generated number from 1 to 100 that must be guessed 
    magicNumber = random.randint(1,100) 
    var1 = 0 
    var2 = 0 
    guess = eval(input('Enter a number: ')) 
    # This loop sets random values for var1 and var2 that sum up to the magicNumber 

    while var1 + var2 != var: 
     var2 = random.randint(1,100) 
     var3 = random.randint(1,100) 
    # an addition problem is made from var1 and var2 to be used as a hint later 
    hint = 'Try adding '+ str(var1)+ ' and '+ str(var2)+'\n' 
    # withinRange checks to see if the users guess is (valid) within the range of 1 to 100 
    # if not, asks for another number until a valid one is provided 
    withinRange = True if (guess <= 100 and guess >= 1) else False 

    while not withinRange: 
     count = count + 1 
     guess = eval(input('Number is invalid.Enter a number between 1 and 100: ')) 
     withinRange = True if (guess <= 100 and guess >= 1) else False 
    # rng tells the user if his guess is too high or low 
    rng = 'low' if guess < magicNumber else 'high' 

    # the following loop repeatedly asks for input until the user enteres the majicNumber 
    while magicNumber != guess: 
     count = count + 1 
     print('\nThats incorrect. Your number is too',rng,end='\n') 
     # after 5 incorrect guesses the program prints out the addition problem that 
     # was made using var1 and var2 
     if count > 5: 
      print(hint) 
     guess = eval(input('Guess: ')) 
     withinRange = True if (guess <= 100 and guess >= 1) else False 
     while not withinRange: 
      count = count + 1 
      guess = eval(input('Nope, has to be between 1 and 100. Try again: ')) 
      withinRange = True if (guess <= 100 and guess >= 1) else False 
     rng = 'low' if guess < magicNumber else 'high' 

    print('\nYou entered the right number!')  

    print('Number:',magicNumber) 
    print('range of last guess was too',rng) 
    print('Number of guesses:',count + 1) 

上次,我被告知,我沒有提供關於我的程序的足夠信息。我希望我沒有在評論中做過。這是我的目標/問題/問題/目標:我想在程序中添加一些代碼,讓它在7次嘗試後終止。

該程序現在所做的是接受猜測一遍又一遍,直到達到正確的。但是我想在'count'變量達到6之後添加一些殺死它的代碼。每次輸入猜測時,count變量都會上升。不管它是否正確。

任何建議將非常感激,感謝提前嚮導!

+3

你有沒有聽說過'break'? –

+0

不要重複自己,你在循環之前和循環中都有相同的代碼。使用'while True:'。 –

+0

我有,但我真的不知道如何將它添加到那裏。我想我可以這樣做,如果count> 6:break'。但是,作爲第一行,我會把它放在哪裏?假設這就是我需要的權利 – user2662440

回答

0

代碼有很多問題。

請不要使用eval()將字符串轉換爲整數。 int()這樣做沒有eval的安全風險。

在您的while循環中,您將var1var2var進行比較。什麼是var?你沒有在任何地方定義它。我想你的意思是magicNumber。然後在循環中設置var2var3。爲什麼var3。你不用它。至少,獲得這兩個數字的最佳方法是獲得一個最大值爲magicNumber的數字,然後只計算第二個數字。

True if (guess <= 100 and guess >= 1) else False可以寫成

True if 1 <= guess <= 100 else False 

擺脫循環的重複代碼的內外循環。您可能需要添加其他功能,例如一個功能只是爲了從用戶那裏獲得輸入並做一些基本的檢查。

+0

...和'如果1 <=猜測<= 100,則返回True,否則False可以寫爲'1 <= guess <= 100'。 – glglgl

+1

它可以 - 而且應該! – Matthias

+0

@Matthias我非常欣賞這些建議/更正。對於一個noob的代碼在開始時會非常糟糕,很難從更有經驗的程序員那裏得到任何實際的幫助。再次感謝,在重新編寫代碼時,我一定會實施您的建議。 – user2662440