Getting Python program error: x is not defined [closed] - python

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I made a program in python to calculate the roots of a quadratic equation by asking user for the coefficients.When I run the program for D>0, it is fine but for D<0 , it is printing imaginary solution but I am also getting the error : x is not defined. I want to remove that.I tried many other loops also but since I am a beginner at my own so I got struck , please help
import math
a=int(input("Enter a:"))
b=int(input("Enter b:"))
c=int(input("Enter c:"))
D = b * b - 4* a* c
if D < 0:
print("imaginary solution")
if D >= 0:
x = (-b + math.sqrt(D)) / (2 * a)
y =- b/a - x
print(x, y)

x is only defined when D >= 0, you have to either also define x when D < 0, or skip any code including x.
Import math
a = int(input("Enter a:"))
b = int(input("Enter b:"))
c = int(input("Enter c:"))
D = b * b - 4 * a * c
if D < 0:
print("imaginary solution")
else:
x = (-b + math.sqrt(D)) / (2 * a)
y = -b/a - x
print(x,y)

the problem is that x is assigned (and defined) only in a specific case,
one solution is to define it at the start of the program, as:
x=0

Related

c++ program runs correctly, but the same python program not [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
My task is:
C2[9]. Given natural number N. Remove from the number the third digit (from left). Example #1. N = 1256; answer: 126. Example #2. N = 432; answer: 43. Example #3. N = 98; answer: 98.
I wrote the code in c++ and it works according to the task. I tried the same task in python but there the output is wrong...
Here is the code in python:
x = 10292989
xx = x
if x <= 99:
print(x)
else:
d=1
while x > 999:
x//10
d**10
x//10
fin = x * d + xx % d
print(fin)
Here is the code in c++:
#include<iostream>
using namespace std;
int c2(int);
int main(){
int x = 10292989;
cout<<c2(x)<<endl;
}
int c2(int x){
int d=1;
int xx = x;
int fin;
if(x <= 99){
return x;
}
else{
while(x > 999){
x /= 10;
d *= 10;
}
x/=10;
return fin = x * d + xx % d;
}
}
Here x//10 changes the variable only in memory, not overwrites the variable, so try (x = x//10):
x = 10292989
xx = x
if x <= 99:
print(x)
else:
d=1
while x > 999:
x = x//10
d = d**10
x = x//10
fin = x * d + xx % d
print(fin)

Turn pseudocode into functioning code to find greatest common divisor [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I have the pseudocode here that says: Initialize d to the smaller of m and n.
While d does not evenly divide m or d does not evenly divide n do
Decrease the value of d by 1
Report d as the greatest common divisor of n and m
and I can't figure out what's preventing my code from working. I'll post my code below, if you could take a look for me and tell me what's preventing my code from working properly I'd appreciate it.
m = int(input("Enter a positive integer: "))
n = int(input("Enter another positive integer: "))
d = min(m,n)
while True:
if d % m != 0 or d % n != 0:
d -= 1
elif d % m == 0 and d % n == 0:
print(d)
break
To find if a value evenly divides another, you need to have the smaller (divisor) on the right side of the modulus operator. So change your code to:
if (m %d != 0) or (n % d != 0):
This evaluates if d evenly divides mor n, instead of if m or n evenly divide d.
The condition in your if is basically the opposite of your elif condition, and to evenly divide m means m should be the left side of the modulo operator (same thing for n). I made an edit here to show what I mean:
m = int(input("Enter a positive integer: "))
n = int(input("Enter another positive integer: "))
d = min(m,n)
while m % d != 0 or n % d != 0:
d -= 1
print(d)

How do I check divisibility in python? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I am writing a python script right now to check if a number a Wilson Prime, but it says I have a syntax error on line 9.
I've tried both the mod function with x%y == 0 and x/y == int, but both of them have given me a syntax error.
n = int(input("Type a natural number here"))
fact = 1
for i in range(1,n):
fact = fact * i
finalfact = fact + 1
if finalfact % n == 0
print(n, "is a prime number!")
if (finalfact/n) % n == 0
print(n, "is also a Wilson Prime!")
I'm trying to make it check if (n-1)!+1 is divisible by n (which is a way to find prime numbers) and to check if (n-1)!+1 is divisible by n^2 (Wilson Primes), but it gives me a syntax error on line 9 and nothing happens.
You are missing : at the end of if statements.
Change:
if finalfact % n == 0
To:
if finalfact % n == 0:
And:
if (finalfact/n) % n == 0
To:
if (finalfact/n) % n == 0:
Need to indent the code properly:
Change:
for i in range(1,n):
fact = fact * i
To:
for i in range(1,n):
fact = fact * i
Also:
finalfact / n # 5 / 2 = 2.5
Should be:
finalfact // n # 5 // 2 = 2

Getting incorrect answer - Sum of Multiples 3 & 5 [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I have looked at other's code, and tried the suggestions but it didn't help. I need to add the multiples of 3 and 5 up to but not including 100 in python.
I've tried searching through StackOverflow already.
def multiples():
total2 = 0
for x in range (1,100):
if (x % 3 == 0) or (x % 5 == 0):
total2 += x
return total2
print(multiples())
It says 3 as my output, which is obviously wrong. What am I doing wrong?
The return statement is inside the loop in the if block, so it's going to return on the first matching number, which is 3.
Simply move it out of the loop:
def multiples():
total2 = 0
for x in range (1,100):
if (x % 3 == 0) or (x % 5 == 0):
total2 += x
return total2
print(multiples())

Writing a program that estimates math constant e without using any functions [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
Write a program that estimates the value of the mathematical constant e by using the formula
[Note: Your program can stop after summing 10 terms.]
e = 1 + 1/1! + 1/2! + 1/3! + ...
I am new to programming languages and trying to learn python by myself. This question has been asked and answered before but I am seeking a solution without any function or module and I want to use only while or for loop.
Edit: This is the code that I wrote for calculating a factorial:
n = 0
factorial = 1
n = raw_input( "Enter a positive integer: ")
n = int(n)
while n < 0:
print "Enter a POSITIVE integer: "
backup = n
backup = int(backup)
while n != 0:
factorial *= n
n -= 1
print "%d!= %d" % (backup, factorial)
And this could be funny to most of you but this is the code that I wrote for the question but it ended up with a syntax error:
accuracy = 1
factorial = 1
counter = 1
e = 1.0
accuracy = raw_input( "Enter desired accuracy of e: ")
accuracy = int (accuracy)
while (counter <= (accuracy - 1))
factorial = factorial * counter
e = e + ( 1 / float(factorial))
counter += 1
print "Constant e is: ", e
Your first step is writing a factorial function. This can be done recursively or with a for-loop:
def factorial(n):
return 1 if n < 2 else n * factorial(n-1)
or:
def factorial(n):
x = 1
for i in range(2, n+1):
x *= i
return x
and then for calculating e we can again use recursion or a for-loop. This function takes a parameter which indicates how many terms in the formula it should evaluate (so you should call with 10 in your case).
def calc_e(t):
if not t:
return 0
else:
return 1 / factorial(t-1) + calc_e(t-1)
or:
def calc_e(t):
s = 0
for i in range(t):
s += 1 / factorial(i)
return s
and they both work:
>>> calc_e(1)
1.0
>>> calc_e(2)
2.0
>>> calc_e(3)
2.5
>>> calc_e(4)
2.6666666666666665
>>> calc_e(5)
2.708333333333333
>>> calc_e(10)
2.7182815255731922
>>> calc_e(20)
2.7182818284590455

Categories

Resources