2013-01-22 31 views
0

注意:我解決了問題

我不想引起任何人浪費自己的時間試圖解決這一問題的代碼,因爲,我發現了這個問題。每個想嘗試幫助的人都會爲此付出代價。蟒蛇,有問題的字符串轉換爲整數GoogleAppEnginge做一個簡單的簡單的數學公式

這是我自己發現這個問題的問題,對於犯這個錯誤感到非常愚蠢。

錯誤在我的HTML代碼中。我不小心將一堆字段命名爲「Number」,並且這樣做沒有變量可供Python腳本使用。

我不敢相信我做了一件簡單的事。我真的很感謝你是努力的人。

另外我想我需要創建一個驗證反正,以防止用戶輸入錯誤的東西,所以我很感激你的建議。

你們搖滾,謝謝你的快速反應。

我會盡力幫助別人,作爲我表達謝意的方式!

嗨我想創建一個非常基本的基本Web應用程序,需要文本輸入,並採取幾個窗體的文本,並將其轉換爲一個整數,所以我可以用它做一些數學方程,然後顯示結果

下面是大部分腳本(跳到底部看到,發生故障的關鍵部分)

class MainPage(webapp.RequestHandler): 
def get(self): 
    self.response.out.write(""" 
     <html> 
     <body> 
      <form action="/battle" method="post"> 
      <div style="color:red"><p> 
<label>Attacking Player Name 
    <input type="text" name="attacker" id="attacker" /> 
</label> 
</p> 
<h3>Attacker's heros</h3> 

<label># of Archers 
<input type="number" name="number" id="archers1" min="0" max="50" /> 
</label> 
<br> 
<label># of Sword Dudes 
<input type="number" name="knight1" id="# of Sword Dudes"min="0" max="50"> 
<br> 
# of Ballistae 
<input type="number" name="ballistae1" id="ballistae1"min="0" max="50"> 
</label> 
<p><br /> 
<label>Defending Player Name 
    <input type="text" name="defender" id="defending" /> 
</label> 
</p> 
<h3>Defender's heros</h3> 
<p><label># of Archers 
<input type="number" name="number" id="archers2" min="0" max="50" /> 
</label> 
<br> 
<label># of Sword Dudes 
<input type="number" name="knight2" id="# of Sword Dudes"min="0" max="50"> 
<br> 
# of Ballistae 
<input type="number" name="ballistae2" id="ballistae1"min="0" max="50"> 
</label> 
</p></div> 

      <div><input type="submit" value="Battle!"></div> 
      </form> 
     </body> 
     </html>""") 


class battle(webapp.RequestHandler): 
def post(self): 
    announce=self.request.get('attacker')+" attacks "+self.request.get('defender') 
    archers1c=int(cgi.escape(self.request.get('archers1'))) 
    knights1c=float(self.request.get('knights1')) 
    ballistae1c=float(self.request.get('ballistae1')) 
    armycount1=archers1+knights1+ballistae1 
    armycount2=self.request.get('archers2')+self.request.get('knights2')+self.request.get('ballistae2') 
    self.response.out.write('<html><body>You wrote:<pre>') 
    self.response.out.write(str(announce)) 
    self.response.out.write(cgi.escape(self.request.get('player1name'))) 
    self.response.out.write('<br><br>') 
    self.response.out.write(armycount1) 
    self.response.out.write('</pre></body></html>') 

好了,這裏是我的問題:

announce=self.request.get('attacker')+" attacks "+self.request.get('defender') 
    archers1c=int(self.request.get('archers1')) 
    knights1c=int(self.request.get('knights1')) 
    ballistae1c=int(self.request.get('ballistae1')) 
    armycount1=archers1+knights1+ballistae1 

時,這名p劇本的藝術來看,我在這條線得到一個錯誤:

archers1c=int(self.request.get('archers1')) 

我收到的錯誤是

ValueError: invalid literal for int() with base 10: '' 

我怎麼轉換是由用戶的形式輸入產生成若干字符串所以它可以在數學方程中使用?

armycount1=archers1+knights1+ballistae1 

我是新來的谷歌應用程序引擎,但不是新的蟒蛇真的,我希望這是一個非常簡單的事情來解決。

+0

什麼*是*您正在傳遞給函數的值?將出現錯誤的那行分成兩行,將self.request.get的內容傳遞給一個臨時變量,然後*查看內容。這是一個可以變成數字的字符串嗎?這是一個空的列表嗎?沒有? etc等 –

+0

@Paul C 爲了使變量易於閱讀,以下是我輸入到表單中的內容。 進攻方名稱:測試 攻擊的英雄 #弓箭手1 #劍花花公子的2 #弩3 的防守隊員姓名 衛士的英雄 #弓箭手1 #劍的花花公子2 弩3 的#這是錯誤: 文件 「/data2/fosdev/app/helloworld.py」,第60行,在交 archers1 = INT(self.request.get( 'archers1')) ValueError:int()與基數10的無效文字:'' 對不起,不熟悉如何在此處設置格式以便輕鬆閱讀所有內容。 – TheWebTech

+0

我修復了代碼,我非常欣賞這種努力。 – TheWebTech

回答

1

您的代碼拋出異常,因爲self.request.get('archers1')正在返回空字符串,可能是因爲表單字段爲空。

我的建議是,以驗證您的表單字段,並使用try/except語句來捕獲異常:

try: 
    archers1c=int(self.request.get('archers1')) 
except ValueError: 
    # inform user of form validation error 
+0

我很感謝你的努力,我自己解決了這個問題,但是你的幫助讓我意識到我需要這樣做,以確保用戶不會在表單中輸入錯誤的東西。 所以我標記你已經回答了 – TheWebTech

+0

這個問題看看wtforms,http://wtforms.simplecodes.com/docs/1.0.2/#但記住你仍然需要在服務器端驗證(wtf也有助於此),你永遠不會相信用戶輸入。 –

相關問題