不瞞各位此問題是小的一項作業但卡關許久,所以前來尋求幫助,題目類似:
1.詢問用戶輸入台幣換美金的匯率值 且用戶必須輸入是float 或empty (but default value is used.) 否則一直詢問用戶輸入匯率值
[我卡關的地方在此,一是不懂如何篩出empty?default? 二是老師不允許沒有輸入值直接按enter 卻可以繼續run ]
2.當輸入匯率值符合要求,則詢問要換多少台幣? 然後計算出可換得美金多少
3.完成計算後再次詢問用戶是否再來一次? 是,則重問
我試過 使用isdigit和isinstance 去篩出float 但卡在 input('give a rate')? 為str 就不知是否該先 float(input)嗎? 但又還是卡empty 和default....
另附上老師的一段提示,但我有看沒有懂
Basically, program will accept only a float value or a null value.
If a float value is provided the program will use the user provided value to do the calculations.
If null value is provided the program will use the default value to perform the calculations.
You will need to set the default value yourself.
Example:
user_value is provided by the user
default_value is set to 1.5
if user_value is null then exchange_rate is set to default_value
otherwise, exchange_rate is set to user_value.
我對題目的解讀如下
試試看合不合用
#You will need to set the default value yourself.
default_value = 1.5
bRun = True
suser_value = input('give a rate')
while bRun == True:
if suser_value:
if suser_value == 'empty':
#If null value is provided the program will use the default value to perform the calculations.
user_value = default_value
else:
#If a float value is provided the program will use the user provided value to do the calculations.
user_value = float(suser_value)
ntd_value = float(input('How many NTD ?'))
print ('USD = ', ntd_value * user_value)
exg_next = input('continue ? (Y to continue)')
if exg_next == 'Y':
bRun = True
suser_value = input('give a rate')
else:
bRun = False
else:
suser_value = input('give a rate')
剛試過沒問題啊
import os
clear = lambda: os.system('cls')
# windows 是 cls , Linux是clear,清屏用
currency = 28.975
a=""
while(len(a)==0):
clear()
a=input("要交換的台幣金額:")
if a.isdigit()==False:
a=""
print("折合美金:{0:.2f}" .format(float(a)/currency))