如何消除python中没有语法错误,程序部分执行但程序未完全执行的错误?


a=2
print("Enter x")
x = int (input())
if (x<=1):
print ("Prime numbers are integers greater than 1.")
else:
for i in range(2,x-1):
remainder = x % i
if remainder == 0 :
status= 0
if status == 0:
print ("x is not a prime number.")
elif status == 1:
print("x is a prime number.")
if status == 0:
print ("Factors of x:")
for z in range (1,x):
if x % z == 0:
print(z)
print  ("Y=8X*X+1 = ",y)
print ("Evaluation of the equation (Y=8X*X+1) for the values of x from -5 to 5:")
for x in range (-5,5):
y = 8 * x ^ 2 + 1

这是一个Python程序:用于判断输入的数字X是否是素数。如果数字是素数,程序将显示它的所有因数。然后计算方程:Y= 8X^2 + 1;

程序正在调试,程序中没有语法错误。程序运行并只接受输入X并结束。整个程序没有执行

修复了所有缩进问题,并添加了一些关于如何修复代码的注释。

status = 1 # Need to instantiate as 1 to assume x is prime
print("Enter x")
x = int (input())
if (x<=1):
print ("Prime numbers are integers greater than 1.")
else:
for i in range(2,x-1):
remainder = x % i
if remainder == 0 :
status= 0

if status == 0:
print ("x is not a prime number.")
elif status == 1:
print("x is a prime number.")

if status == 0:
print ("Factors of x:")
for z in range (1,x):
if x % z == 0:
print(z)

print ("Evaluation of the equation (Y=8X*X+1) for the values of x from -5 to 5:")
for x in range (-5,5):
y = 8 * x ** 2 + 1 ## exponential operator is ** in python, ^ is bitwise or operator
## Need to define y first before the print statement
print("Y=8X*X+1 = ",y) 

代码:

#Removed the 'a' variable as you are not using anywhere in the code.
#You can write 'Enter x' while taking input from the user also no need of print statement here
x=int(input("Enter x:"))
if(x<=1): 
print("Prime numbers are integers greater than 1.")
else:
# Verifiying x is prime number or not.
flag=1 
for i in range(2,x-1):
if x%i==0:
print("x is not a prime number.")
flag=0
break  #You can simply break as number is not prime
#AS given in description you have to find factors only for prime number
if flag:    
print("x is a prime number.")
print ("Factors of x:")
#just a point if you wanted factors of only prime number no need of for loop you can directly write the factors [1,number_itself]
for z in range(1,x+1): #Number_itself is also a factor hence you need to iterate till x not till x-1. In otherwords in range you should iterate till x+1..
if x % z == 0:
print(z)
#Now finding all values of y where x is -5 to 5  Note* In range parameter {end} you have right 5.. instead you should right 6. as what we right in {end} parameter is not included in the iteration..              
print("Evaluation of the equation (Y=8X*X+1) for the values of x from -5 to 5:")
for x in range (-5,6):
y=8*(x**2) + 1

print("At x ="+str(x)+" Value of y = "+str(y))

输出:

非素数

Enter x:28
x is not a prime number.
Evaluation of the equation (Y=8X*X+1) for the values of x from -5 to 5:
At x =-5 Value of y = 201
At x =-4 Value of y = 129
At x =-3 Value of y = 73
At x =-2 Value of y = 33
At x =-1 Value of y = 9
At x =0 Value of y = 1
At x =1 Value of y = 9
At x =2 Value of y = 33
At x =3 Value of y = 73
At x =4 Value of y = 129
At x =5 Value of y = 201

质数

Enter x:17
x is a prime number.
Factors of x:
1
17
Evaluation of the equation (Y=8X*X+1) for the values of x from -5 to 5:
At x =-5 Value of y = 201
At x =-4 Value of y = 129
At x =-3 Value of y = 73
At x =-2 Value of y = 33
At x =-1 Value of y = 9
At x =0 Value of y = 1
At x =1 Value of y = 9
At x =2 Value of y = 33
At x =3 Value of y = 73
At x =4 Value of y = 129
At x =5 Value of y = 201

最新更新