我只是在编写一个简单的货币转换器程序,我面临的一个问题是我无法想到如何让用户更改变量的内容。以下是基本位:
def D2Y():
def No():
newconv=float(input("What is the rate that you want to use: "))
amount=float(input("How much do you want to convert: $"))
conversionn=round(amount*newconv,2)
print("¥",conversionn)
return (rerun())
def Yes():
conrate2=float(97.7677)
amount=float(input("How much do you want to convert: $"))
conversion=round(amount*conrate2,2)
print("¥",conversion)
return (rerun())
def rerun():
rerun=int(input("Do you want to go again?\n1 - Yes\n2 - No\nChoice: "))
if rerun==1:
return (main())
else:
print("Thank you for converting")
conrate1=int(input("The currency conversion rate for Yen to 1 US Dollar is ¥97.7677 to $1\n1 - Yes\n2 - No\nDo you want to use this rate?: "))
if conrate1==1:
return (Yes())
else:
return (No())
我不知道你是怎么做到的。我不介意你摆脱def功能。
答案 0 :(得分:0)
这是一种取消功能并允许您修改汇率的方法:
rate = CONVERSION_RATE = 97.7677
while True:
print "The currency conversion rate for Yen to 1 US Dollar is ¥%.4f to $1" % rate
print 'Select an option:\n1)Modify Conversion Rate\n2)Convert a value to dollars\n0)Exit\n'
choice = int(raw_input('Choice: '))
if choice == 0:
print("Thank you for converting")
break
if choice == 1:
try:
rate = float(raw_input("What is the rate that you want to use: "))
except ValueError:
rate = CONVERSION_RATE
amount = float(raw_input("How much do you want to convert: $"))
print "¥", round(amount * rate,2)
print '\n\n'
在使用中,它看起来像这样:
The currency conversion rate for Yen to 1 US Dollar is ¥97.7677 to $1
Select an option:
1)Modify Conversion Rate
2)Convert a value to dollars
0)Exit
Choice: 2
How much do you want to convert: $1.00
¥ 97.77
...
Choice: 1
What is the rate that you want to use: 96.9000
How much do you want to convert: $1.00
¥ 96.9