2015-09-11 50 views
-1

我需要更改哪些內容才能使此程序起作用?我得到:可以使用提供的代碼向我解釋字符串文字嗎?

line 10 
SyntaxError: Non-UTF-8 code starting with '\x92' in file C:/Users/RobotAdmin/PycharmProjects/untitled3/dialouge on line 10, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details 

我不知道這是什麼意思。

def main(): 
    phrases = "Thank You Please Hello Hi How Old Are You What Is Your Address"**  # String literal 

    print(phrases[17:22]) # This statement prints a specific segment of characters from the string literal 
    name = input('What is your name? :')  # This statement prompts the user to enter their name and accepts it 
    print(phrases[23:25] + " " + name)  # Retrieves from the string literal,and combines it with the name 
    print(phrases[25:41]) # This Statement asks for the user’s age 
    age = input('Age in Years? :') # This statement prompts the user to enter their age and accepts the age the entered 
    print(phrases[42:64]) # This segment asks What the user’s address is 
    address = input("Street Address, City, State, Country") #This statement asks user to enter their address 
    print ("" 
      "______________________________" 
      "|       |" 
      "|",name,"     |" 
      "|       |" 
      "|",age,"      |" 
      "|       |" 
      "|",address,"     |" 
      "|       |" 
      "|       |" 
      "|____________________________|") 
+1

你谷歌錯誤或訪問它告訴你要訪問的網站? – TigerhawkT3

+0

您的輸出框的右側看起來會錯開,因爲在輸出空格以證明框時沒有考慮「name」,「age」或「address」的長度。另外,你爲什麼使用單個字符串作爲'短語'而不是一串字符串? – chepner

回答

1

獲取錯誤的原因是因爲user’s註釋中使用的字符user's這是標準ASCII的微妙差異。所以最簡單的解決方案就是改變這兩個角色。

如果您希望保留它們,你只需要一個合適的編碼添加到您的腳本的頂部:

# -*- coding: utf-8 -*- 

要修復腳本輸出,一種解決方案是使用字典存儲你的變量。這有兩個好處,首先您可以輕鬆確定輸入的所有文本的長度。其次,它可以與format語句一起用於替換輸出中的佔位符。

有了一些額外的(很奇怪的)符號,我們可以讓Python爲所有排隊自動添加足夠的空間或下劃線填充。

def main(): 
    phrases = "Thank You Please Hello Hi How Old Are You What Is Your Address"  #**  # String literal 

    details = dict() 
    print(phrases[17:22]) # This statement prints a specific segment of characters from the string literal 
    details['name'] = input('What is your name? :')  # This statement prompts the user to enter their name and accepts it 
    print(phrases[23:25] + " " + details['name'])  # Retrieves from the string literal,and combines it with the name 
    print(phrases[25:41]) # This Statement asks for the user's age 
    details['age'] = input('Age in Years? :') # This statement prompts the user to enter their age and accepts the age the entered 
    print(phrases[42:64]) # This segment asks What the user's address is 
    details['address'] = input("Street Address, City, State, Country") #This statement asks user to enter their address 

    # Determine the length of the longest entry 
    max_width = max([len(v) for v in details.values()]) 

    print (""" 
__{0:_<{width}}__ 
| {0: <{width}} | 
| {name:{width}} | 
| {0: <{width}} | 
| {age:{width}} | 
| {0: <{width}} | 
| {address:{width}} | 
| {0: <{width}} | 
| {0: <{width}} | 
|_{0:_<{width}}_|""".format('', width=max_width, **details)) 

main() 

這將顯示類似以下的輸出:

_____________________________ 
|       | 
| Fred Flintstone   | 
|       | 
| 35      | 
|       | 
| Cobblestone Lane, Bedrock | 
|       | 
|       | 
|___________________________| 
-1

首先,如果您在列表中打破了您的短語而不是拼接字符串,那麼它的可讀性會更好。

phrases = ['Hello', 'Thank You', 'How old are you'] #rest of phrases  
print (phrases[0]) #prints out "Hello" 

其次,我嘗試了你的代碼已經究竟是如何貼吧,但在姓名,年齡和地址虛值而不是堵塞,和代碼,但沒有任何錯誤(儘管可能不符合格式是你可能期望)。它所談論的Unicode字符('\ x92')是一個逗號。你有任何外來的逗號?

+0

你從哪裏得到'\ x92'是逗號的想法? – chepner

相關問題