2013-10-10 43 views
0

我已經在這幾個小時,尋找問題,甚至要求我的兄弟,誰是EE的高級,甚至他被難倒了。這是在午夜前的一個項目,我似乎無法擺脫這個錯誤。任何人都可以看看我的代碼,並找出爲什麼我一直得到這個錯誤?謝謝,是的,它們在while語句下面正確縮進。Python3雖然語法錯誤

# Initialize Variables 
count = 0 

#First Customer Input 
cus = input("Enter customer's name: ") 
custype = input("Enter customer's service type (R for Residential. B for Business): ") 
location = input("Enter customer's service location (C for City. P for Parish): ") 
kwh = eval(input("Enter customer's number of Kilowatt Hours Used: ") 

#Repeat loop based on input items NOT being the sentinel value 
while (count < 10): 
    if custype == 'R' and location1 == 'C': 
     rate = residentialCity 
    elif custype == 'R' and location1 == 'P': 
     rate = residentialParish 
    elif custype == 'B' and location1 == 'C' or 'P': 
     rate = business 
+0

你能編輯你的文章以包含代碼中的確切縮進嗎? Python對它很挑剔。它確實看起來像是缺少'kwh = eval(input ...'行中的右括號,這是一個非常糟糕的想法BTW。 –

+0

您的代碼未被格式化爲代碼顯示(我們已經爲您修復了它)要正確地格式化代碼,選擇它並點擊'{}'圖標,或者縮進4列 –

+0

@ D.Shawley是對的如果你想輸入一個數字,使用'int(input(...))'或'float(input(...))'。有一個原因是Python在3.0中擺脫了自動評估的輸入函數,不要把它帶回來 – abarnert

回答

5
kwh = eval(input("Enter customer's number of Kilowatt Hours Used: ") 

算括號。

而D.Shawley在評論中指出,將用戶輸入傳遞給eval可以做任意壞事。無論如何,你爲什麼使用eval

+1

一般來說,任何時候你有一個'SyntaxError'看起來完全有效的那一行,而且不管muc如何,都會得到相同的'SyntaxError'你從那條線上剝離出來,並且在其他地方複製並粘貼的同一行很好......問題在於它上面的行缺少關閉paren,括號,大括號或引號(或者有一個流浪的反斜線)。 – abarnert

+0

謝謝。那就是訣竅。我不能相信我錯過了這一點,儘管對我來說有一點公平,那就是「錯誤」的四行。 –

+1

@DanielBoudreaux:這是你經過幾次調試後學習的東西之一,然後它變成第二天性。希望得到這個竅門,你只需要經歷一次。 – abarnert

3

如上州的答案,你錯過了一個括號有:

kwh = eval(input("Enter customer's number of Kilowatt Hours Used: ") 

爲什麼你會需要一個eval聲明?看起來像你期待一個數字,並希望將字符串更改爲一個整數。爲什麼不這樣做呢?

kwh = int(input("Enter customer's number of Kilowatt Hours Used: ")) 

只是一個建議,但除非有沒有更好的辦法,不要用eval。這是不安全和不穩定的,有人可能意外地或不會輸入代碼,並且會混淆你的程序。

+0

有人已經寫過這個問題的答案,並且評論關注了eval的使用。你甚至引用了另一個答案。這個答案沒有提供真正的價值,這就是爲什麼我下調了。 –

+0

那麼,在我的辯護中,我只建議他使用'int',因爲那可能是他想要的。 – aIKid

+0

雖然我很看重你的意見。謝謝。 – aIKid