2017-10-23 5 views
0

私はヒントの電卓を作っていますが、入力がフロートではなかったためにメッセージを出力するようにしたいのですが、そのメッセージを出力してから元の入力と再試行するように私は何かのように行うことができます:出力に依存するif文を作成したい場合はどうすればよいですか? (Imを実行しているPython)

if print("No stupid use a number."): 
     return input("Bill before tax.") 

答えて

0

私はこれがうまくいくと思います。

def calc_tip(total): 
    total = float(total) 
    return total * 0.1, total * 0.15, total * 0.2 


while True: 
    try: 
     tip10, tip15, tip20 = calc_tip(input("Enter total before tax: ")) 
    except ValueError as e: 
     print("input a number") 
    else: 
     break 

print('Tip amount: 10%% = $%0.2f, 15%% = $%0.2f, 20%% = $%0.2f' % 
     (tip10, tip15, tip20)) 
関連する問題