Python 错题集

#题目: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("")

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值