为什么忽略if语句?

时间:2014-10-07 15:05:16

标签: python

我用C ++和Java编写代码。我的表弟需要帮助,我从未体验过Python,并且决定是时候在试图解决这个问题的几个小时之后得到stackoverflow的帮助了。为什么在编译和运行程序时没有读取最后一个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

2 个答案:

答案 0 :(得分:7)

最后if语句的条件是userInput是否等于'g'。鉴于userInput始终是一个浮点数,因为它在输入/赋值时被转换,因此该条件不可能评估True

答案 1 :(得分:2)

你的最后一部分是:

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

userInput是一个浮点数,所以它永远不会是'g'