为什么python给了我热不热?

时间:2015-07-11 22:13:13

标签: python

temp = 120
if temp > 85:
   print "Hot"
elif temp > 100:
   print "REALLY HOT!"
elif temp > 60:
   print "Comfortable" 
else:
   print "Cold"

1 个答案:

答案 0 :(得分:3)

Python选择第一个条件为true,并跳过其余 var StudentModel = Backbone.Model.extend({ defaults: { name: 'Volodya', lastName: 'Peterson', age: 22, gender: 'male' } }); var StudentsCollection = Backbone.Collection.extend({ model: StudentModel, url: '/students.json' }); var StudentView = Backbone.View.extend({ tagName: 'li', template: _.template($('#studentTpl').html()), events: { 'click': function () { eventAggregator.trigger('student:selected', this.model); } }, render: function () { this.$el.html(this.template(this.model.toJSON())); return this; } }); var GroupView = Backbone.View.extend({ tagName: 'ul', initialize: function () { this.collection = new StudentsCollection(); this.collection.on('update', this.render, this); this.collection.fetch(); }, render: function () { var self = this; this.collection.each(function (student) { var studentView = new StudentView({ model: student }); self.$el.append(studentView.render().el); }); $('body').html(this.$el); } }); var RouterView = Backbone.View.extend({ tagName: 'ul', render: function () { var self = this; _.each(this.model.toJSON(), function (value) { self.$el.append('<li>' + value + '</li>'); }); return this; } }); var GroupController = function () { this.start = function () { var groupView = new GroupView(); }; }; var Router = Backbone.Router.extend({ routes: { '': 'index', 'student/:name': 'viewStudent' }, index: function () { groupController.start(); }, viewStudent: function (name) { var groupView = new GroupView(); var selectedStudent = groupView.collection.find(function (student) { return student.get('name') === name; }); $('body').append((new RouterView({ model : selectedStudent})).render().el); } }); var eventAggregator= _.extend({}, Backbone.Events), groupController; $(function () { var router = new Router(); groupController = new GroupController(); Backbone.history.start(); eventAggregator.on('student:selected', function (student) { var urlpath= 'student/'+ student.get('name'); router.navigate(urlpath, {trigger: true}); }); }); 个分支。

if..elif..else为真,因此第一次测试通过并打印120 > 85。在那一点之后第二次测试也匹配并不重要。

首先放置'Hot'测试

> 100

或者,限制测试以排除上限范围:

if temp > 100:
   print "REALLY HOT!"
elif temp > 85:
   print "Hot"
elif temp > 60:
   print "Comfortable" 
else:
   print "Cold"