2014-06-27 87 views
3
import random 

def usertype(): 
    randletter = random.choice('qwer') 
    userinput = raw_input('Press '+str(randletter)) 
    if userinput == randletter: 
     return 'Correct' 
    else: 
     return 'Incorrect' 

def usertypetest(x,y,result): 
    while x <= 9: 
     result = usertype() 
    if result == 'Correct': 
     x = x+1 
     y = y+5 
    else: 
     x = x+1 
     y = y-2 
    return str(y)+'is your score' 

print usertypetest(0,0,usertype) 

這是我的代碼。我希望它要求用戶按下一個按鈕,從集合(Q,W,E,R)中隨機選擇一個按鈕,然後打印正確或不正確,具體取決於他們按下哪個按鈕。我希望這發生10次。十次嘗試後,它會打印他們的分數:每個'正確'爲5,'錯誤'爲-2。相反,我收到這個。程序卡在while循環不打印

Press e(e) 
Press e(e) 
Press w(e) 
Press q(e) 
Press q(e) 
Press q(e) 
Press r(e) 
Press e(e) 
Press w(e) 
Press q(e) 
Press e(e) 
Press e(e) 
Press e(e) 
Press e(e) 
Press q(e) 
Press w(e) 
Press r(e) 
Press w(e) 
Press r(e) 
Press w(e) 
Press r(e) 
Press r(e) 

無論我輸入什麼,它都不會返回「正確」或「不正確」。它也在過去10年繼續,並沒有顯示他們的分數。顯然有一個問題我沒有看到。

我的輸入在括號內。

爲了澄清,這就是我想要的:

Press q(q) 
Correct 
Press e(q) 
Incorrect 
Press w(w) 
Correct 
Press q(q) 
Correct 
Press e(eq) 
Incorrect 
Press e(e) 
Correct 
Press q(q) 
Correct 
Press q(r) 
Incorrect 
Press w(w) 
Correct 
Press r(r) 
Correct 
29 is your score 
+2

你有壓痕錯誤 – Fabricator

回答

3

在Python縮進非常重要

在該代碼中,xwhile循環從未改變作爲if塊是在相同的縮進水平爲while循環。所以,唯一的環形指令result = usertype()

while x <= 9: 
    result = usertype() 
if result == 'Correct': 
    x = x+1 
    y = y+5 

兩個額外的批評:

您遞增x兩個地,當只需要進行一次。

while x <= 9: 
    result = usertype() 
    if result == 'Correct': 
     y = y+5 
    else: 
     y = y-2 
    x += 1 

而且,由於你是循環的固定次數,爲什麼不忽略遞增x並使用一個for循環,像這樣:

for x in range(10): 
    result = usertype() 
    if result == 'Correct': 
     y = y+5 
    else: 
     y = y-2 
+1

儘管參考9,原始循環實際上迭代10次,所以更換需要'範圍(10)'。但是使用'range()'確實更合理。 –

+0

大家好。 [Fencepost錯誤](http://en.wikipedia.org/wiki/Off-by-one_error#Fencepost_error) –

2

你需要把if result == 'Correct':while x <= 9:循環,你得到了用戶的輸入,所以它得到每一次評估下。你可以添加print(result)得到正確/不正確的反饋,在你的榜樣:

def usertypetest(x,y,result): 
    while x <= 9: 
     result = usertype() 
     if result == 'Correct': 
      x = x+1 
      y = y+5 
     else: 
      x = x+1 
      y = y-2 
     print(result) 
    return str(y)+'is your score' 
0

只要把ifwhile循環下。 問題解決了。

嘗試使用此代碼:

import random 

def usertype(): 
    randletter = random.choice('qwer') 
    userinput = raw_input('Press '+str(randletter)) 
    if userinput == randletter: 
     return 'Correct' 
    else: 
     return 'Incorrect' 

def usertypetest(x,y,result): 
    while x <= 9: 
     result = usertype() 
     if result == 'Correct': 
      x = x+1 
      y = y+5 
     else: 
      x = x+1 
      y = y-2 
    return str(y)+'is your score' 

print usertypetest(0,0,usertype) 
1

你的主要問題是,你有錯範圍內的if/else塊。你需要它在while區塊下。這可以確保它在每次運行usertype()時檢查用戶是否輸入了正確的輸入。

import random 

moves = 0 
score = 0 

def usertype(): 
    randletter = random.choice('qwer') 
    userinput = raw_input('Press '+str(randletter)) 
    if userinput == randletter: 
     return True 
    else: 
     return False 

def usertypetest(moves, score): 
    while moves < 10: 
     result = usertype() 
     moves = moves + 1 
     if result: 
      score = score + 5 
     else: 
      score = score - 2 
    return str(score) + ' is your score' 

print usertypetest(moves, score) 
1

此外,不打印變量的結果的值。評估結果後,添加以下內容:

打印結果

0

這裏有幾個問題。

  1. 您需要在usertype返回前打印'正確'。
  2. 你不需要把'結果'放在usertypetest中。
  3. 把if ... else放在usertypetest的循環中。
  4. 更改上次打印。

這裏是正確的代碼。

import random 

def usertype(): 
    randletter = random.choice('qwer') 
    userinput = raw_input('Press '+str(randletter)) 
    if userinput == randletter: 
     print 'Correct' 
     return 'Correct' 
    else: 
     print 'Incorrect' 
     return 'Incorrect' 

def usertypetest(x,y): 
    while x <= 9: 
     result = usertype() 
     if result == 'Correct': 
      x = x+1 
      y = y+5 
     else: 
      x = x+1 
      y = y-2 
    return str(y)+'is your score' 

print usertypetest(0,0) 
1

除了其他人已經確定的縮進問題之外,代碼並不是特別慣用的Python。該usertypetest()功能可能是:

def usertypetest(x,y,result): 
    for x in range(10): 
     if usertype() == 'Correct': 
      y = y + 5 
     else: 
      y = y - 2 
    return '%d is your score' % y 

可能有更好的方式來做到這一點,但這是稍微簡單一些,多一些Python化。

我還會觀察到,我沒有看到示例聲稱看到的輸入周圍的括號。

如果你想看到每個字母的判決,那麼你需要拯救usertype()畢竟返回:

def usertypetest(x,y,result): 
    for x in range(10): 
     result = usertype() 
     print result 
     if result == 'Correct': 
      y = y + 5 
     else: 
      y = y - 2 
    return '%d is your score' % y 
+0

感謝您向我展示如何簡化它。儘管第一個回答者生成了功能性代碼,但您爲了簡化而獲得+1。此外,我只是把括號放在那裏以確定輸入,它們不在實際輸出中。 – Bretsky