2014-10-07 193 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