Home > database >  How to remove the error in python when there is no syntax error and program is executing partially b
How to remove the error in python when there is no syntax error and program is executing partially b

Time:01-19

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

This is a Python program: used to determine whether a number entered ,X, is prime or not. If the number is prime the program will display all its factors. Then it will evaluate the equation: "Y= 8X^2 1" for the values of from -5 to 5.

The program is debugging and there is no syntax error in the program. The program runs and only take the Input X and ends. The whole program is not executing.

CodePudding user response:

Fixed all the indentation issues and added some comments on how to fix your code.

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) 

CodePudding user response:

Code:

#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))

Output:

Not a prime number

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

Prime number

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
  • Related