#题目:https://www.python123.io/student/courses/7150/groups/94959/problems/programmings/6080#pagetop
#我的代码
money = input("请输入货币:")
if money[0,4] in ["RMB"]:
A = eval(money[-1,4])*6.78
print("{:.2f}USD".format(A))
elif money[0,4] in ["USD"]:
B = eval(money[-1,4])/6.7
print("{:.2f}RMB".format(B))
else:
print("")
#参考答案
CurStr = input()
if CurStr[:3] == "RMB":
print("USD{:.2f}".format(eval(CurStr[3:])/6.78))
elif CurStr[:3] in ['USD']:
print("RMB{:.2f}".format(eval(CurStr[3:])*6.78))
错误1、保留字 "in"用法错误, in用于判断,列表里仅有“RMB”,无需使用判断语句。
2、print("USD{:.2f}".format(eval(CurStr[3:])/6.78)) 中,{:.2f}的f就是format函数里的值,可以是一个式子可以是一个变量
3、eval函数中的money[-1,4]的切片错误,切片的一位数所对应字符的位置应该在第二位数的前面,不能交叉使用,交叉使用的后果提出的序列是空序列。
4、审题不仔细,换算错误。正确代码如下
money = input()
if money[:3] == "RMB":
A = eval(money[3:])/6.78
#A 与format是同一个A吗?需要统一A\USD\format函数中的A吗
print("USD{:.2f}".format(A))
elif money[:3] == "USD":
B = eval(money[3:])*6.78
print("RMB{:.2f}".format(B))
else:
print("")