在最后一个for循环中,当满足Y = 0时,我将排除执行循环。我该怎么做?

时间:2018-09-09 17:18:47

标签: python python-3.x

import matplotlib.pyplot as plt
import numpy as np
import math

epsr = float(input('Enter the relative permittivity of waveguide material ')) #permittivity
mur=float(input('Enter the relative permeabilty of waveguide material ')) # permeability
f0= float(input('Enter the operating frequency ')) #operating frequency
LWG= float(input('Enter the length of the waveguide ')) #length of waveguide
rho=float(input('Enter the conductivity of the waveguide material '))# Conductivity
thickness=float(input('Enter the thickness of the waveguide ')) #Thickness
c= 300000000 #Speed of light
lamda0=c/f0 #wavelength in free space
mu0=1.25663706e-6 #permeability of free space
eps0= 8.854187817e-12 # permittivity of free space
k=2*math.pi*math.sqrt(mur*epsr)/lamda0 ##wavenumber
print(k)
N= float(input('Enter the number of slots ')) #No of slots
ln= float(input('Enter the length of the slot ')) #Length of the slot
xn= float(input('Enter the offset of the slot ')) #offset of the slot
#Width of the waveguide
a=float(input('Enter the width of the waveguide ie long dimension  '))
#Height of the waveguide 
b=float(input('Enter the height of the waveguide ie short dimension '))
Z0=120*math.pi #impedence of free space
print(Z0)
AOS=(a*b-(a-2*thickness)*(b-2*thickness)) #area of cross section of the waveguide
print(AOS)
G0=rho*AOS/LWG #Conductance of the waveguide
fc=c/(2*a) #cut off frequency
nsa = 1/N #normalized slot admittance
beta=2*math.pi*f0*math.sqrt(1-(pow((fc/f0),2)))/c #factor beta
fn=(math.cos(beta*ln)-math.cos(k*ln))*math.sin(math.pi*xn/a)/math.sin(k*ln)
K1=-1j*math.sqrt(8*(a/b))/(pow(math.pi,2)*Z0*G0*(beta/k))
#K1=pow(math.pi,2)*Z0*G0*(beta/k)
nsv=nsa/(K1*fn*math.sin(k*ln))
lamdag=lamda0/math.sqrt(1-pow((fc/f0),2))
theta = np.linspace(0, 180, 1001)
G=[]
AF=[]
F=[]
print(theta)
for i in  range(1001):
    if math.sin(theta[i])==0:
        G.insert(i,0)
    else:
        p=(math.cos(k*ln*math.cos(theta[i]))- 
math.cos(k*ln))/math.sin(theta[i])
        G.insert(i,p)
for q in  range(1001):
    if math.cos(theta[q])==0:
        AF.insert(q,0)
    else:
        m=pow(math.e,(1j*N*k*lamdag/2*math.cos(theta[i])))
        AF.insert(q,m)
for n in  range(1001):
        h=N*nsv*G[n]*AF[n]
        Y=h.real
        if
        Y=0
        print(Y)
        p=math.log10(Y)
        F.insert(n,p)

这是用于开发矩形阵列辐射图的代码。如果日志遇到0,因为log10(0)未定义,则会收到错误消息。对于上述主题的任何帮助将不胜感激。

按以下顺序使用输入:
1.461100000000000.5353800000.0540.05,{{ 1}},0.0050.2

2 个答案:

答案 0 :(得分:3)

我认为您说的是“如果Y不等于0,那么...”,从程序上来说是:

if Y != 0

更新循环。我猜想您也希望在Y为0时也避免插入F中。

for n in  range(1001):
        h=N*nsv*G[n]*AF[n]
        Y=h.real
        if Y!=0:
            print(Y)
            p=math.log10(Y)
            F.insert(n,p)

答案 1 :(得分:0)

上一个循环的这一部分

    if
    Y=0
    print(Y)
    p=math.log10(Y)
    F.insert(n,p)

在语法上不正确;您可能是说

    if Y==0:
        print(Y)
    p = math.log10(Y)
    F.insert(n, p)

但是log10(0)仍将被求值,因此添加else分支:

    if Y==0:
        print(Y)
    else:
        p = math.log10(Y)
        F.insert(n, p)

,如果您也想打印Y的非零值,请将print()函数放在if之前:

    print(Y)
    if Y==0:
        pass             # do nothing, but syntactically something here is needed
    else:
        p = math.log10(Y)
        F.insert(n, p)

但是在这种情况下,您不需要两个分支,因此只需取反您的条件即可:

    print(Y)
    if Y != 0:
        p = math.log10(Y)
        F.insert(n, p)

或-由于if语句中的非零值被评估为True

    print(Y)
    if Y:
        p = math.log10(Y)
        F.insert(n, p)

请注意,我根据PEP 8 -- Style Guide for Python Code在您的代码中做了一些小的样式更改。