关于input类型的思考
来源:5-5 项目作业
慕九州4205410
2020-12-26 06:07:42
# 具体遇到的问题
如果输入字符串就会报错,因为只有把输入转换成int类型才能跟service_menu比较,怎么才能解决这个冲突呢?

# 尝试过的解决思路和结果
尝试过取消int,但跟字典就比较不出来
# 粘贴全部相关代码,切记添加代码注释(请勿截图)
在这里输入代码,可通过选择【代码语言】突出显示
# coding:utf-8
service_menu = {1: '人民币转换美元', 2: '美元转换人民币', 3: '人民币转换欧元', 0: '结束程序'}
# 各个兑换汇率
USD_to_RMB = 7.06
RMB_to_EUR = 0.12
print('************欢迎使用货币转换服务系统************')
# 利用items函数调取service_menu中的key and value
for key, value in service_menu.items():
print(key, value)
while True:
Your_Choice = input('请您选择需要的服务:')
if int(Your_Choice) == 1:
print('~~~~~~~~~~~~~~~~~~~~~~~~\n欢迎使用人民币转换美元服务')
your_money = int(input('请输入需要转换的人民币金额:'))
print('您需要转换的人民币为:%s' % your_money)
print('兑换成美元为:', round(your_money / USD_to_RMB, 2), '$', sep='') # sep用于消除print中的空格,round用于取小数点后两位
print('========================')
if input('是否继续兑换:') == '是':
continue
else:
break
elif int(Your_Choice) == 2:
print('~~~~~~~~~~~~~~~~~~~~~~~~\n欢迎使用美元转换人民币服务')
your_money = int(input('请输入需要转换的美元金额:'))
print('您需要转换的美元为:{}'.format(your_money))
print('兑换成人民币为:', round(your_money * USD_to_RMB, 2), '$', sep='') # sep用于消除print中的空格,round用于取小数点后两位
print('========================')
if input('是否继续兑换:') == '是':
continue
else:
break
elif int(Your_Choice) == 3:
print('~~~~~~~~~~~~~~~~~~~~~~~~\n欢迎使用人民币转换欧元服务')
your_money = int(input('请输入需要转换的人民币金额:'))
print(f'您需要转换的人民币为:{your_money}')
print('兑换成欧元为:', round(your_money * RMB_to_EUR, 2), '$', sep='') # sep用于消除print中的空格,round用于取小数点后两位
print('========================')
if input('是否继续兑换:') == '是':
continue
else:
break
elif int(Your_Choice) == 0:
print('~~~~~~~~~~~~~~~~~~~~~~~~\n感谢您的使用,祝您生活愉快,再见!')
break
else:
print('~~~~~~~~~~~~~~~~~~~~~~~~\n信息有误,请重新输入')
continue
1回答
好帮手慕小轩
2020-12-26
同学,你好!将输入转换成int类型与service_menu比较是正确的,输入字符串就会报错的原因是,同学输入的汉字不能转换成整数类型,要输入数字1、2、3、0进行程序运行
加油,祝学习愉快~~~
相似问题