我在2.7有一個工作貨幣轉換器,但我想確保程序不會獲取它無法處理的用戶輸入的數據。如何使用break語句
- 如何獲取用戶輸入要理解不相關的情況下
- 如何讓程序重新啓動,如果用戶輸入錯誤的;即如果中斷,但我不知道如何做到這一點,儘管四處搜索並測試了一些方法。
我已經留下了其餘的代碼,因爲它是微不足道的,有效地使用預設數字複製第一組乘法。
currency = str(raw_input ("""what currency would you like to covert: GBP, EURO, USD OR YEN?
"""))
exchange = str(raw_input("""what currency would you like in exchange? : GBP, EURO, USD OR YEN?
"""))
amount = int(input("""how much would you like to convert?
"""))
decision = str(raw_input("""Please enter u for user input exchange rate or s for the preset exchange rate
"""))
if decision == "u" :
user_rate = raw_input("Please enter the current exchange rate")
exchange_value = int(amount) * int(user_rate)
print ("At the user found exchange rate you will receive",exchange_value,exchange)
elif decision == "s" :
if currency == "GBP" and exchange == "USD":
exchange_value= int(amount) * 1.6048
print ("At the preset exchange rate you will receive",exchange_value,exchange)
if currency == "GBP" and exchange == "EUR":
exchange_value= int(amount) * 1.2399
print ("At the preset exchange rate you will receive",exchange_value,exchange)
有沒有循環。你不能從非循環中突破。 – Makoto 2013-05-11 16:21:48
無關:'str(raw_input(...))'是多餘的,因爲'raw_input'返回一個字符串。 'int(input())'應該是'int(raw_input())'。 – bereal 2013-05-11 17:31:00