2015-04-21 62 views
0

你好,我有三個問題,我的代碼:無限循環X計數器問題

  1. 當我爲我的第一個問題鍵入「N」,它進入一個錯誤。
  2. 「Run again?」後我會進入一個無限循環嗎?輸入。
  3. 我的計數器沒有合適的加起來,所以即使我得到的答案是對還是錯,它都不算它們。

請幫幫我。

下面是我的代碼:

#Introduction-ish print statement 
print("In this application, we will be playing a coin coss game. For as "\ 
     "many times as you like, we will continue playing the game.") 


#def function1(): 
response=str(input("\nWould you like to run this application? Type 'Y' to run "\ 
       "or 'N' to not run: ")) 
if response=="N": 
    print("\nOutcome of Game:") 
    print("You did not run the application."\ 
      " Nothing happened. You did not play the game.") 


#Counters 
programCounter=0 
hCounter=0 
tCounter=0 
guessCountR=0 
guessCountW=0 

#User Input 

if response=="Y": 

    funcGuess=str(input("\nPick one- Type 'H' for Heads or 'T' for Tails: ")) 

#Coins 

import random 

#number=random.randint(1,2) 

while response !="N" and funcGuess=="H" or funcGuess=="T": 
    number=random.randint(1,2) 
    if number==1: 
     number=="Heads" 
     hCounter+=1 
    else: 
      number=="Tails" 
      tCounter+=1 
    if funcGuess==number: 
     guessCountR+=1 
    else: 
     guessCountW+=1 

    print(number) 
    response=str(input("Run again?")) 
    if response=="Y": 
     funcGuess=str(input("\nPick one- Type 'H' for Heads or 'T' for Tails: ")) 
if response=="N": 
    print("\nOutcome of Game:") 
    print("You guessed ",programCounter, "time(s).") 
    print("You guessed heads",hCounter, "time(s).") 
    print("You guessed tails",tCounter, "time(s).") 
    print("You guessed correctly",guessCountR, "time(s).") 
    print("You guessed incorrectly",guessCountW, "time(s).") 

#Guess Count 
guessCount=guessCountR+guessCountW 


#paste 


#if response =="Y": 
    #function1() 

#else: 
    #print("\nOutcome of Game:") 
    #print("You did not run the application."\ 
      #" Nothing happened. You did not play the game.") 

我不介意的所有評論。我故意把他們留在那裏,但如果有人幫助他們發現他們有用,請讓我知道。

對長期職位的先進的道歉。

+0

請爲每個問題創建一個最簡單的例子,並將它們作爲單獨的問題重新提交。 – TheBlackCat

回答

0

問題1:

在線34你正在評估一個名爲funcGuess變量。如果你對最初的問題回答'Y',它只會在第26行定義。

問題2:

while循環無限循環,因爲中斷條件不能得到滿足。沒有辦法在循環內指定response值「N」。此外,一定要放括號之間的or評價雙方以評估正確

問題3行:

正確的數量和猜測被賦值給變量numberfuncGuess,分別。 number始終爲1或2,funcGuess始終爲「H」或「T」。因此funcGuess == number永遠不能評估爲True