2014-10-07 187 views
-4

我用C++和Java編寫代碼。我的表弟需要幫助,我從來沒有經歷過Python,並且決定在花費數小時試圖解決這個問題之後得到stackoverflow的幫助。爲什麼編譯和運行程序時最後的IF語句沒有被讀取?我試圖打印一行和總數。沒有任何工作。這裏是代碼: 編輯:我來幫助學習和尋求一種方向感,爲什麼會有人低估我的聲譽。它使人們不再提問而且不太友好。爲什麼if語句被忽略?

'''      Project 1 

    Room Types    Room Rates    Extra Bed Charges   

G = Garden View   Garden View = $185/day  Garden View Room = $15/day 
P = Pool View   Pool View = $195/day  Pool View Room = $15/day 
L = Lake View   Lake View = $215/day  Lake View Room = $20/day 
                Golf Course  = $25/day 
''' 

garden_view_rate = 185 
pool_view_rate = 195 
lake_view_rate = 215 
seniorDiscount = .10 


TaxRates = { 'State Tax':0.085} 

roomTotal=extraBed=TotalDue=0 


userInput = '' 
loopAgain = True 

print 'Welcome to the Ritz Carlton Hotel! Please follow the menu below as our options have recently changed.' 

while loopAgain: 

    print '\nG = Garden View' 
    print 'P = Pool View' 
    print 'L = Lake View' 


    userInput = raw_input('Please enter the abbreviation of the desired room type: ').lower() 

    if userInput not in ['g','p','l'] : 

     print 'Invalid room type, please try again.' 
     continue 

    num_days = raw_input('Please enter the number of days guest will stay: ') 

    userInput = raw_input('Guest need an extra bed? (Y/N) ').lower() 

    if userInput not in ['y','n'] : 

     print 'Invalid option, please try again.' 
     continue 

    userInput = raw_input('Will the guest use the Golf Course? (Y/N) ').lower() 

    if userInput not in ['y','n'] : 

     print 'Invalid option, try again.' 
     continue 

    userInput = float(raw_input('Please enter the age of the guest: ')) 


    if userInput == 'g' : 
     Amount = int(garden_view_rate + TaxRates['State Tax']) 
     print 'Your total balance is: ', Amount 
+9

你爲什麼想到'float'值等於一個字符串對象? – 2014-10-07 15:08:09

+4

嘗試對不同的輸入使用不同的變量,而不是重複使用相同的'user_input'變量。這在Python中與在C++或Java中不同。 – Barmar 2014-10-07 15:12:35

+0

將嘗試更改變量名稱。非常好的寶馬,Bamar。過於注重語法等。威爾修理它。我現在有一種方向感。 – 2014-10-07 18:08:47

回答

7

最後if語句有一個條件檢查,如果userInput等於'g'。鑑於userInput將始終是一個浮動,因爲它在輸入/賦值時被轉換爲這樣,所以該條件不可能評估True

+0

謝謝。欣賞所需的時間來回答這個問題。 – 2014-10-07 18:15:28

2

你的最後一個部分是:

userInput = float(raw_input('Please enter the age of the guest: ')) 
if userInput == 'g' : 

userInput是浮動,所以它永遠是「G」

+0

謝謝。完全忽略了這一點。 – 2014-10-07 18:16:03