我被分配制作货币转换器,我不确定它为什么不起作用。 这是该计划:
print(
"Welcome to Joseph Pilotte's Currency Converter"
)
USD = input(
"What amount of USD would you like to convert to another currency? Write only the number and click enter "
)
Currency = input(
"Which currency would you like to convert USD from? (Options: The Canadian Dollar(CAD), The Swiss Franc(CHF), The Japanese Yen(JPY), or The British Pound(GBP) PLEASE ENTER 3 LETTER CURRENCY CODE IN UPPERCASE"
)
if Currency == "CAD":
print(USD * 1.252910,
" Canadian Dollars")
if Currency == "CHF":
print(USD * 0.94122, " Swiss Franc")
if Currency == "JPY":
print(USD * 109.29, " Japanese Yen")
if Currency == "GBP":
print(USD * 0.71832,
"British Pounds")
这个问题已经解决了我没有在输入之前将“USD”定义为int
答案 0 :(得分:2)
我冒昧地用一些“改进”重新制作你的节目。
下面:
from textwrap import dedent
curr_dict = {'CAD':1.252910,
'CHF':0.94122,
'JPY':0.71832,
'GBP':109.29}
def float_input(text):
while True:
try:
return float(input(text))
except ValueError:
print('Not a number! Insert correct value.')
def conversion_func(text):
while True:
currency = input(text)
if currency in curr_dict:
return curr_dict.get(currency)
else:
print('Not a valid currency. Try again.')
def main():
'''
Asks for amount to convert (USD) and desired currency
Prints the result
'''
amount = float_input(dedent('''\
Welcome to Joseph Pilotte's Currency Converter
What amount of USD would you like to convert to another currency?
Write only the number and click enter\n'''))
conversion = conversion_func(dedent('''\n\
Which currency would you like to convert USD from?
(Options: The Canadian Dollar(CAD), The Swiss Franc(CHF),
The Japanese Yen(JPY) or The British Pound(GBP)
PLEASE ENTER 3 LETTER CURRENCY CODE IN UPPERCASE'''))
print(amount*conversion)
main()