我无法弄清楚为什么在代码行之后不打印(如果6 <= n <= 20)

时间:2019-07-06 07:31:31

标签: python-3.x if-statement

我正在运行此代码。当我将输入设为6或更大时,它不会打印任何内容,也不会显示任何错误。就像它在其后不打印任何内容,如果6 <= n <= 20

我尝试重新编写代码或错误,并经过多次检查。

#!/bin/python3

import math
import os
import random
import re
import sys



if __name__ == '__main__':
    n = int(input().strip())
    if n%2 == 0 :
        if 2 <= n <= 5:
            print("Not Weird")
    elif n%2 == 0:
        if 6 <= n <= 20 :
            print("Weird")
    elif n%2 == 0:
        if n > 20:
             print("Not Weird")
    else:
        print("Weird")

它没有显示任何错误

2 个答案:

答案 0 :(得分:0)

您没有正确使用if。检查一下,

if __name__ == '__main__':
    n = int(input().strip())
    if n%2 == 0 :
        if 2 <= n and n <= 5:
            print("Not Weird")

        elif 6 <= n and n <= 20 :
            print("Weird")
        elif n > 20:
             print("Not Weird")
    else:
        print("Weird")

您的代码有什么错误?

ANS:

if n%2 == 0: 
    some condition
elif n%2 ==0:
    some condition

您的ifelif条件都是相同的。因此,它永远不会执行elif条件。

因此,在您的情况下,n=6进入第一个if状态。因为您的1st if条件中没有其他陈述。它不会print达到预期的效果。

答案 1 :(得分:0)

尝试以下操作:

    const express = require('express');
    const app=express();

    const async=require("async");
    const waterfall=require('async-waterfall');

    const TIMESHEET_DETAILS = require('../Models/slt_timesheet');

    //For filing the timesheet
          router.post('/postTimesheet',(req,res)=>{
            let monthFromClient=req.body.month;
            let projectArrayFromClient=req.body.projectArray;
            let activityArray=req.body.activityArray;
            let hoursArrayForAMonth=req.body.hoursArray;

            let currentDateObject = new Date();
            let lastMonthDateObject= new Date(currentDateObject.getFullYear(),currentDateObject.getMonth()+1,0);
            let nowMonth = monthList[lastMonthDateObject.getMonth()];
            let thisYear = lastMonthDateObject.getFullYear();
            let response;
            let onlyTimeSheetData=[];
            let timesheetRecord;

            try {
               //First check here request month is same as the current method
              if(monthFromClient===nowMonth && req.session.email!=null){
           //async waterfall starts here 
                waterfall([
                    function getTimesheet(callback){
                      console.log('waterfall execution starts here');

                      try {
                        timesheetRecord = TIMESHEET_DETAILS.findOne({email:req.session.email});
                        console.log(timesheetRecord.email);
                        // onlyTimeSheetData=timesheetRecord.timesheetData[0];
                      } catch (e) {
                        console.log(e);
                        callback(e,null);
                        return;
                      }

                      console.log(onlyTimeSheetData);
                      callback(null,onlyTimeSheetData);
                    },

                    function insertNewDataInFormat(err,onlyTimeSheetData) {
                      if(err){
                        console.error(err);
                      }else {
                        console.log(onlyTimeSheetData);
                      }

                    }
                ]);
                response='successful';
              }else {
                response='not successful';
              }
            } catch (e) {
              console.error('error find in main catch'+e);
            } finally {
              res.send(response);
            }