2014-10-02 64 views
-3
while RUN2: 
    chose = int(input('Type the option number from 1 to 6:')) 

    if chose == 1: 
     print(grossPayMsg()) 
     print("Your gross pay is", grosspay(workhour, hourlywage))  

    elif chose == 2:  
     print(withHoldingsMsg()) 
     print("Your withholdings is", withhd(workhour, hourlywage))  

    elif chose == 3: 
     print(federalTaxMsg()) 
     print("Your Federal Tax is", fedtax(workhour, hourlywage)) 

    elif chose == 4:  
     print(marylandTaxMsg()) 
     print("Your Federal Tax is", mdtax(workhour, hourlywage)) 

    elif chose == 5:  
     print(netPayMsg()) 
     print("Your Federal Tax is", net(workhour, hourlywage)) 

    elif chose == 6: 
     print ("Thank you for using. Goodbye!") 
     RUN2 = False 

    else: 
     print("/////// Invalid option number ///////") 

============================================= =========================爲什麼我的python代碼拋出ValueErrors異常?

Q。如何在輸入不輸入選項號碼時避免如下所示的ValueErrors?我知道有些用戶可能會故意或意外地輸入密碼。是否有任何聲明可以避免這種情況?與基地10個無效字面INT():我只學會INT(輸入)或輸入...

ValueError異常:無法將字符串轉換爲浮動

ValueError異常 ''

==== =================================

非常感謝你提前......

+0

避免「ValueErrors」你可以用一個try-except塊(https://docs.python.org/2/tutorial包裝您的輸入語句/errors.html),而'None'是因爲你調用了一個沒有return語句的函數,但是你正在打印它返回的任何內容 – 2014-10-02 04:08:25

+0

歡迎來到StackOverflow。由於您的帖子包含多個問題(請每個問題只有一個問題)和代碼審閱請求(這裏有一個單獨的網站http://codereview.stackexchange.com/),您似乎收到了密切的投票。也許你想編輯你的問題,或刪除它,並從一個或多個特定的,非重複的問題開始,爲此可以選擇一個清晰,詳盡,正確的答案作爲您的首選解決方案。 – tripleee 2014-10-02 04:29:05

回答

1

您可以通過更改該代碼刪除none

if chose == 1: 
     print(grossPayMsg()) 
     print("Your gross pay is", grosspay(workhour, hourlywage)) 

if chose == 1: 
      grossPayMsg() # print is removed because when you use print it prints the return value of grossPayMsg() 
      print("Your gross pay is", grosspay(workhour, hourlywage)) 

可以使用strng.isnumeric()功能檢查輸入的字符串是否是數字。之後,轉換成float並將其存儲在workhourhourlywage

strng = input("What's your hours worked?:") 
if strng.isnumeric() : 
    workhour = float(strng) 
相關問題