0

## python Q : 篩出類行是 float or empty 但default也接受?! 請拯救我枯竭的腦袋

1.詢問用戶輸入台幣換美金的匯率值 且用戶必須輸入是float 或empty (but default value is used.) 否則一直詢問用戶輸入匯率值
[我卡關的地方在此，一是不懂如何篩出empty?default? 二是老師不允許沒有輸入值直接按enter 卻可以繼續run ]
2.當輸入匯率值符合要求，則詢問要換多少台幣? 然後計算出可換得美金多少
3.完成計算後再次詢問用戶是否再來一次? 是，則重問

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.

froce iT邦大師 1 級 ‧ 2020-11-05 08:23:26 檢舉

input得到的結果就是str，你得轉型成float，沒問題的話拿這個值當匯率...

toro523 iT邦新手 5 級 ‧ 2020-11-05 14:12:33 檢舉

### 2 個回答

0

iT邦大神 1 級 ‧ 2020-11-05 09:37:11

``````#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')
``````
toro523 iT邦新手 5 級 ‧ 2020-11-05 14:16:40 檢舉

1
japhenchen
iT邦大師 1 級 ‧ 2020-11-05 08:54:12

``````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))
``````

toro523 iT邦新手 5 級 ‧ 2020-11-05 14:16:06 檢舉

toro523 iT邦新手 5 級 ‧ 2020-11-05 15:23:55 檢舉