Python 可以用提供的代码向我解释字符串文字吗?
我需要更改什么才能使此程序正常工作?我得到:Python 可以用提供的代码向我解释字符串文字吗?,python,pycharm,Python,Pycharm,我需要更改什么才能使此程序正常工作?我得到: 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
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"
其次,我尝试了您发布代码的方式,但是插入了name、age和address的伪值,代码运行时没有出现错误(尽管可能没有使用您希望的格式)。它所说的Unicode字符('\x92')是逗号。您是否有任何多余的逗号?出现错误的原因是您的
用户的注释中使用了字符,
,这与标准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()
您是否搜索了错误或访问了它告诉您要访问的网站?输出框的右侧看起来会错开,因为在输出空格以对齐框时,您没有考虑到名称
、年龄
、或地址
的长度。还有,为什么要用一个字符串来表示短语而不是字符串列表?你从哪里知道\x92
是逗号?
_____________________________
| |
| Fred Flintstone |
| |
| 35 |
| |
| Cobblestone Lane, Bedrock |
| |
| |
|___________________________|