2014-10-16 2 views
-2

Начинает с python здесь.Программа Python производит неправильный вывод

Просто написал простую программу, но результат неверен. Я должен упустить что-то очень основное.

Пример входных данных:

Prody 
1000000 
3000 

Выход:

Hi Prody 
The property price is: 1000000 
The estimated rental cost for the property is: 3000 
The price to rental ratio for the property = 27 
This is an expensive property. 

Но выход должен быть:

Hi Prody 
The property price is: 1000000 
The estimated rental cost for the property is: 3000 
The price to rental ratio for the property = 27 
This property is a bit expensive. 

Что случилось?

Спасибо!

Код:

information = {'name': '0', 'cap': 0, 'rent': 0} 

print "First you need to provide some information." 
information['name'] = raw_input("What is your name?") 
information['cap'] = raw_input("What is the cost of the property?") 
information['rent'] = raw_input("What is the estimated rent for the property?") 

print "\n" 
print "Hi", information['name'] 
print "The property price is: ", information['cap'] 
print "The estimated rental cost for the property is: ", information['rent'] 
print "\n" 

class Property(object): 

    def enter(self): 
     pass 

class Overpriced(Property): 
    def testprice(self): 
     print "The price to rental ratio for the property =", 
       int(information['cap'])/(12 * int(information['rent'])) 

     if tprice > 40: 
      print "This is an expensive property." 
     elif (tprice > 25) and (tprice < 40): 
      print "This property is a bit expensive." 
     else: 
      print "Testing." 

tprice = Overpriced() 
tprice.testprice() 

Спасибо за комментарии, изменил последнюю часть к:

class Overpriced(Property): 
    def testprice(self): 
     rental_ratio = int(information['cap'])/(12 * int(information['rent'])) 
     print "The price to rental ratio for the property =", rental_ratio 

     if rental_ratio > 40: 
      print "This is an expensive property." 
     elif (rental_ratio > 25) and (rental_ratio < 40): 
      print "This property is a bit expensive." 
     else: 
      print "Testing." 

tprice = Overpriced() 
tprice.testprice() 
+0

Вы понимаете, какое значение имеет значение 'tprice'? 'tprice' - это объект, который присваивается' Overpriced() '. Используйте разные переменные. –

+0

Как вы можете сравнить «экземпляр класса» с значением? –

+0

спасибо за обзор первоначального сообщения @sammy – Prody

ответ

0

Вы вычислил значение, и напечатал его, но позже вы Ждут не использовать его. Это исправление заставит его работать :)

class Overpriced(Property): 
def testprice(self): 
    computed_price = int(information['cap'])/(12 * int(information['rent'])) 
    print "The price to rental ratio for the property =",computed_price 


    if computed_price > 40: 
     print "This is an expensive property." 
    elif (computed_price > 25) and (computed_price < 40): 
     print "This property is a bit expensive." 
    else: 
     print "Testing." 

tprice = Overpriced() 
tprice.testprice() 
+0

Кажется, теперь это работает :) Вы должны посмотреть имена переменных, и вы должны быть в порядке :) – Beri

+0

Спасибо, это работает! – Prody

Смежные вопросы