I'm running the following code from the command line (python filename.py) and it wont terminate. I've tried the code outside of a procedure and have tried the procedure in an online interpreter, so I don't think it's the the algorithm. What am I doing wrong?
n = raw_input("Enter a number: ")
def print_multiplication_table(n):
x = 1
while x <= n:
y = 1
while y <= n:
z = x * y
print x, " * ", y, " = ", z
y += 1
x += 1
print_multiplication_table(n)
You should convert the number received from raw_input into an integer. Right now it's being compared as a string.
An easy (but probably bad) way to do this:
n = int(raw_input("Enter a number: "))
There is a problem with the raw_input command. I have a similar code myself (guess we're both following the Udacity course). I tried to add the raw_input line to my code, and it ended up in an infinite loop too.
Related
Here is the question my teacher gave us:
Write a program to find the sum of the following series (accept values of x and n from user). Use functions of the math library like math.pow and math.factorial:
1 + x/1! + x2/2! + ……….xn/n!
This is the code I've come up with. I calculated it on paper with simple numbers and there's clearly some logic errors because I can't get a correct answer. I think I've been looking at it for too long to really grasp the issue, so I could use some help.
import math
x = int(input ("Input x: "))
n = int(input("Input n: "))
for i in range (n):
power = math.pow(x,i)
ans = 1 + (power / math.factorial(i))
print(ans)
Note: This is an entry level class, so if there's an easier way to do this with a specific function or something I can't really use it because I haven't been taught it yet, although appreciate any input!
I would not recommend using those functions. There are better ways to do it that are within the grasp of entry level programmers.
Here's what I recommend you try:
import math
x = int(input ("Input x: "))
n = int(input("Input n: "))
sum = 1
term = 1.0
for i in range (1,n+1):
term *= x/i
sum += term
print(sum)
You don't need those functions. They're inefficient for this case.
I believe the following will work for you:
import math
x = int(input ("Input x: "))
n = int(input("Input n: "))
ans = 1
for i in range (1,n+1):
power = math.pow(x,i)
ans += (power / math.factorial(i))
print(ans)
We start the ans variable at 1 and we add the new term to ans through each iteration of the loop. Note that x+=n is the same as x=x+n for integers.
I also changed the range that your loop is going through, since you start at 1 and go up to n, rather than starting at 0 and going to n-1.
Output:
Input x: 2
Input n: 5
7.266666666666667
For people familiar with the mathematical constant e:
Input x: 1
Input n: 15
2.718281828458995
Your ans only includes (the 1 and) the latest term. You should instead Initialize it before the loop and then add all the terms to it. The first term 1 doesn't deserve ugly special treatment, so I compute it like all the others instead:
import math
x = int(input ("Input x: "))
n = int(input("Input n: "))
ans = 0
for i in range(n + 1):
ans += math.pow(x, i) / math.factorial(i)
print(ans)
And a preview how you'll likely soon be taught/allowed to write it:
import math
x = int(input ("Input x: "))
n = int(input("Input n: "))
print(sum(math.pow(x, i) / math.factorial(i)
for i in range(n + 1)))
I'm new to python and attempting to write a linear equation using Cramer's Rule. I've entered the formula and have code prompting user to enter a,b,c,d,e and f but my code is getting a syntax error. I'd like to fix the code and also have a system for researching and correcting future errors.
a,b,c,d,e,f = float(input("Enter amount: ")
a*x + by = e
cx + dy = f
x = ed-bf/ad-bc
y=af-ed/ad-bc
if (ad - bc == 0)print("The equation has no solution")
else print ("x=" x, "y=" y,)
Basically, your code was one giant syntax error. Please read some basic tutorial, as was suggested in the comments. Hopefully this will help in the learning process (I didn't go through the actual math formulas):
a = float(input("Enter a: "))
b = float(input("Enter b: "))
c = float(input("Enter c: "))
d = float(input("Enter d: "))
e = float(input("Enter e: "))
f = float(input("Enter f: "))
##a*x + by = e
##cx + dy = f
if (a*d - b*c == 0):
print("The equation has no solution")
else:
x = (e*d-b*f)/(a*d-b*c)
y = (a*f-e*d)/(a*d-b*c)
print ("x=%s" % x, "y=%s" % y)
You have to put * between the numbers you want to multiply. You had one parenthesis missing from your input statement. The equations themselves are commented out, because otherwise Python takes them as a code (incorrectly written one). You have to enclose the denominator in parentheses because math. You have to end the if, else, elif, for, while and such with :. Indentation is VERY important in Python.
I'm writing a basic program to convert any 4 digit number in reverse.
I know I'm taking a very complex approach, but that's what my professor requires. So far I've got:
print("This program will display any 4-digit integer in reverse order")
userNum = eval(input("Enter any 4-digit integer: "))
num1 = userNum % 10
userNum2 = userNum // 10
num2 = userNum2 % 10
userNum3 = userNum // 100
num3 = userNum3 % 10
userNum4 = userNum // 1000
num4 = userNum4 % 10
print(num1,num2,num3,num4)
The issue I'm having is the output from the print statement gives me
x x x x
When I would prefer to have
xxxx
Any advice?
If you read the description of the print(), you can see that you can change your last line for:
print(num1,num2,num3,num4, sep='')
Since you just want to convert the input in reverse order. You can take the following approach.
print("This program will display any 4-digit integer in reverse order")
userNum = input("Enter any 4-digit integer: ")
reverse_input = userNum[::-1]
reverse_input = int(reverse_input) # If you want to keep input as an int class
print(reverse_input)
If you want to use your own code then just change the print statement.
print(str(num1) + str(num2) + str(num3) + str(num4))
String concatenation does not add space so you should get desired result.
I used this as a sort of exercise for myself to nail down loops. There are a few loops you can use.
#if using python <3
from __future__ import print_function
x = 3456
z = x
print('While:')
while z > 0:
print(z % 10, end="")
z = z / 10;
print()
print('For:')
for y in xrange(4):
z=x%10
print(z, end="")
x = x / 10
Thanks for asking this question. It encouraged me to go look at python syntax, myself, and I think it's a good exercise.
def add(x,y):
y = int( input("Enter First number "))
x = int( input("Enter Second number "))
answer = x + y
print (answer)
def subtract(x,y):
answer = x - y
print (answer)
y = int ( input("Enter First number "))
x = int ( input("Enter Second number "))
operation = input("Subtract or Add ")
if operation == "add":
add(x,y)
else:
subtract(x,y)
I keep getting an error saying variables x and y aren't being used. Please help. I have been stuck on this for a while now.
You have problems with your scope. You can't call x or y before calling the function as those variables are declared inside the function. Do it once at a time. First you ask what function. Then once inside the function you ask for x and y
def add():
x = int( input("Enter First number "))
y = int( input("Enter Second number "))
answer = x + y
print (answer)
def subtract():
x = int ( input("Enter First number "))
y = int ( input("Enter Second number "))
answer = x - y
print (answer)
operation = input("subtract or add ")
if operation == "add":
add()
else:
subtract()
welcome to Stack Overflow and welcome to Python.
As you might know, in Python indents are really important, as they define which code belongs into which block.
Looking at your request, I must assume that this is a reflection of your code. So I think if you go with the following indentation, it might do what you want:
def add(x,y):
answer = x + y
return answer # Please notice how i use RETURN to return a value from the function call
def subtract(x,y):
answer = x - y
return answer
y = int ( input("Enter First number "))
x = int ( input("Enter Second number "))
operation = input("Subtract or Add ")
result = None
if operation == "add":
result = add(x,y) # Please notice how I store what the function returns!
else:
result = subtract(x,y)
if (result != None):
print result
else:
print "There is no result!"
Please read the comments and ask if you have any more questions.
Maybe you want to consider an elaborate introduction to Python
Your code has many problems and it is really confusing.
as ppperry commented, indentation .When writing python, you should use exact 4 space as indentation.
you did not realize the difference between input and raw_input.
If you are using python2, and your input is not a number, input while try to eval your input. written in python doc.
If you are using python3, you do not need to worry about this because in python3 there is no more raw_input, and input equals old raw_input. This was asked here
follow enpenax's answer. you should first define x and y before you call them.
What is wrong with this code that I'm trying to run in Python? (Pretend that indenting isn't an issue):
def main():
print("This program illustrates a chaotic function")
x = eval(input("Enter a number between 0 and 1: "))
for i in range(10):
x = 3.9 * x * (1 - x)
print(x)
main()
As you can imagine, this has been pretty confusing since I entered it exactly as my textbook shows it. Thanks for the help!
You are doing two wrong things with your code.
1 :
You must not use eval with input method because eval ask for string as input while with input you are returning float value.
You can simply run your program if you are passing float as input.
x = input("Enter a number between 0 and 1: "))
You need to use raw_input(raw_input will return string data)
x = eval(raw_input("Enter a number between 0 and 1: "))
2 :with for loop you need to provide indentation.
Need to indent the function block
def main():
print("This program illustrates a chaotic function")
x = eval(input("Enter a number between 0 and 1: "))
for i in range(10):
x = 3.9 * x * (1 - x)
print(x)
main()
Also, instead of eval I would just use float:
def main():
print("This program illustrates a chaotic function")
x = float(input("Enter a number between 0 and 1: "))
for i in range(10):
x = 3.9 * x * (1 - x)
print(x)
main()
Sample:
>>> def main():
... print("This program illustrates a chaotic function")
... x = float(input("Enter a number between 0 and 1: "))
... for i in range(10):
... x = 3.9 * x * (1 - x)
... print(x)
...
>>> main()
This program illustrates a chaotic function
Enter a number between 0 and 1: .2
0.624
0.9150336
0.303213732397
0.823973143043
0.565661470088
0.958185428249
0.156257842027
0.514181182445
0.974215686851
0.0979659811419
I think your problem was because you run it in sublime text editor
Try running it from command line
$ python yourscript.py
and you'll see that your script run normally.
EOFError that you get was caused by sublimtext that didn't send any input to your program while the built-in input function asked for an input.