我有一个上学项目,我完成了我的代码。代码的目的是用户输入一个单词(使用无限循环来不断检查输入),然后使用格式说明符和“[:: - 1]打印该单词。 “
这是我的代码:
window.open()
当我运行代码时,例如单词“monkey”,这就是我得到的:
b = 1
while b == 1:
a = input("Type a word to format. Typing quit will exit the program: ")
if a == "quit":
break
print("Goodbye!")
else:
print("{[0]:><20}{[1]:<>20}".format(a, a[::-1]))
结果应该如下所示:
Type a word to format. Typing quit will exit the program: monkey
m>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<e
Type a word to format. Typing quit will exit the program:
我不确定我做错了什么。任何意见或建议都非常感谢!
答案 0 :(得分:1)
您不希望替换字段周围有方括号:
>>> a = 'monkey'
>>> print("{0:><20}{1:<>20}".format(a, a[::-1]))
monkey>>>>>>>>>>>>>><<<<<<<<<<<<<<yeknom
在更新的版本(python2.7 +,python3.2?+)中,您甚至不需要指定字段编号(默认情况下它们是枚举的):
>>> a = 'monkey'
>>> print("{:><20}{:<>20}".format(a, a[::-1]))
monkey>>>>>>>>>>>>>><<<<<<<<<<<<<<yeknom