有人可以用提供的代码向我解释字符串文字吗?
Can some explain string literals to me with the provided code?
我需要更改什么才能使该程序运行?我得到:
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," |"
"| |"
"| |"
"|____________________________|")
首先,如果你在一个列表中分解你的短语而不是拼接一个字符串,那么可读性会更好。
phrases = ['Hello', 'Thank You', 'How old are you'] #rest of phrases
print (phrases[0]) #prints out "Hello"
其次,我完全按照您发布的方式测试了代码,而是插入了姓名、年龄和地址的虚拟值,并且代码 运行 没有错误(尽管可能不是您可能需要的格式)。它所谈论的 Unicode 字符 ('\x92' ) 是一个逗号。你有任何 ext运行eous 逗号吗?
出现错误的原因是 user’s
评论中使用的字符 ’
与标准 ASCII 的 user's
有细微差别。所以最简单的解决方案就是只更改这两个字符。
如果您希望保留它们,只需在脚本顶部添加合适的编码即可:
# -*- coding: utf-8 -*-
要修复脚本输出,一种解决方案是使用字典来存储您的变量。这有两个好处,首先,您可以轻松确定输入的所有文本的长度。其次,它可以与 format
语句一起使用以替换输出中的占位符。
通过一些额外的(相当奇怪的)符号,我们可以获得 Python 自动添加足够的 space 或下划线填充到所有行。
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 |
| |
| |
|___________________________|
按原样使用下面的内容并将其插入代码之上。
# coding: utf8
注意:必须在顶部。
我需要更改什么才能使该程序运行?我得到:
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," |"
"| |"
"| |"
"|____________________________|")
首先,如果你在一个列表中分解你的短语而不是拼接一个字符串,那么可读性会更好。
phrases = ['Hello', 'Thank You', 'How old are you'] #rest of phrases
print (phrases[0]) #prints out "Hello"
其次,我完全按照您发布的方式测试了代码,而是插入了姓名、年龄和地址的虚拟值,并且代码 运行 没有错误(尽管可能不是您可能需要的格式)。它所谈论的 Unicode 字符 ('\x92' ) 是一个逗号。你有任何 ext运行eous 逗号吗?
出现错误的原因是 user’s
评论中使用的字符 ’
与标准 ASCII 的 user's
有细微差别。所以最简单的解决方案就是只更改这两个字符。
如果您希望保留它们,只需在脚本顶部添加合适的编码即可:
# -*- coding: utf-8 -*-
要修复脚本输出,一种解决方案是使用字典来存储您的变量。这有两个好处,首先,您可以轻松确定输入的所有文本的长度。其次,它可以与 format
语句一起使用以替换输出中的占位符。
通过一些额外的(相当奇怪的)符号,我们可以获得 Python 自动添加足够的 space 或下划线填充到所有行。
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 |
| |
| |
|___________________________|
按原样使用下面的内容并将其插入代码之上。
# coding: utf8
注意:必须在顶部。