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.
Related
When I run the program below, it outputs all the numbers as 0.0. How can fix this to illustrate chaotic behaviour?
# A simple program illustrating chaotic behaviour
def main():
print ("This program illustrates a chaotic function")
x = int (float(input("Enter a number between 0 and 1: ")))
for i in range(10):
x = 3.9 * x * (1-x)
print (x)
main()
In the line where you get input, you take the number and make it an integer. Making something an integer removes all decimals. When you type in ‘0.54’, then the int function takes off the decimals leaving just 0.
If you just do x = float(input("Enter a number between 0 and 1: ")) then it will work.
Have a nice day!
Actually u have a +1 or - 1 in end will help
This question already has answers here:
What is the purpose of the return statement? How is it different from printing?
(15 answers)
Closed 6 months ago.
I need to design a calculator with the following UI:
Welcome to Calculator!
1 Addition
2 Subtraction
3 Multiplication
4 Division
Which operation are you going to use?: 1
How many numbers are you going to use?: 2
Please enter the number: 3
Please enter the number: 1
The answer is 4.
This is what I have so far:
print("Welcome to Calculator!")
class Calculator:
def addition(self,x,y):
added = x + y
return sum
def subtraction(self,x,y):
diff = x - y
return diff
def multiplication(self,x,y):
prod = x * y
return prod
def division(self,x,y):
quo = x / y
return quo
calculator = Calculator()
print("1 \tAddition")
print("2 \tSubtraction")
print("3 \tMultiplication")
print("4 \tDivision")
operations = int(input("What operation would you like to use?: "))
x = int(input("How many numbers would you like to use?: "))
if operations == 1:
a = 0
sum = 0
while a < x:
number = int(input("Please enter number here: "))
a += 1
sum = calculator.addition(number,sum)
I seriously need some help! All the tutorials on calculators in Python 3 are far more simple than this (as in it only take 2 numbers and then simply prints out the answer).
I need help getting the functions in the Calculator class I made to work. When I try running what I have so far, it lets me input my numbers and whatnot, but then it just ends. It doesn't run the operation or whatever. I am aware I only have addition so far, but if someone can just help me figure out addition, I think I can do the rest.
The code as it is does not work. In the addition function you return the variable sum which will conflict with the build in sum function.
So just return the added and in general avoid the sum, use something like sum_ :
This works fine for me:
print("Welcome to Calculator!")
class Calculator:
def addition(self,x,y):
added = x + y
return added
def subtraction(self,x,y):
diff = x - y
return diff
def multiplication(self,x,y):
prod = x * y
return prod
def division(self,x,y):
quo = x / y
return quo
calculator = Calculator()
print("1 \tAddition")
print("2 \tSubtraction")
print("3 \tMultiplication")
print("4 \tDivision")
operations = int(input("What operation would you like to use?: "))
x = int(input("How many numbers would you like to use?: "))
if operations == 1:
a = 0
sum_ = 0
while a < x:
number = int(input("Please enter number here: "))
a += 1
sum_ = calculator.addition(number,sum_)
print(sum_)
Running:
$ python s.py
Welcome to Calculator!
1 Addition
2 Subtraction
3 Multiplication
4 Division
What operation would you like to use?: 1
How many numbers would you like to use?: 2
Please enter number here: 45
Please enter number here: 45
90
Your program takes input, runs a series of operations, and then ends without ever displaying the result. Try something like print(sum) after the while loop ends.
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.
When i run through my calculator, it gives the following results;
Select operation.
1.Add
2.Subtract
3.Multiply
4.Divide
Enter choice(1/2/3/4):3
Enter first number: 1
Enter second number: 5
Invalid! Input
Can anyone explain to me why it is responding with my else if statement, i'v checked the code many times, plus i have copied paste the code directly, as is, after much frustration, yet it yields the same result?
# A simple calculator that can add, subtract, multiply and divide.
# define functions
def add(x, y):
"""This function adds two numbers"""
return x + y
def subtract(x, y):
"""This function subtracts two numbers"""
return x - y
def multiply(x, y):
"""This function multiplies two numbers"""
return x * y
def divide(x, y):
"""This function divides two numbers"""
return x / y
# Take input from the user
print ("Select operation.")
print ("1.Add")
print ("2.Subtract")
print ("3.Multiply")
print ("4.Divide")
choice = input("Enter choice(1/2/3/4):")
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
if choice == '1':
print(num,"+",num2,"=", add(num1,num2))
elif choice == '2':
print(num1,"-",num2,"=", subtract(num1,num2))
elif choice == '3':
print(num1,"*",num2,"=", multiply(num1,num2))
elif choice == '4':
print(num1,"/",num2,"=", divide(num1,num2))
else:
print("Invalid! Input")
You're using Python 2, where input() evaluates what is entered; so when you enter 2, for instance, choice contains the int 2. Try entering '2' into your current code (including the quotes). It'll act as you expect entering 2 to act.
You should use raw_input() on Python 2 and input() on Python 3. If you want your code to be compatible with both, you can use the following code, after which you can always just use input():
try:
input = raw_input # Python 2
except NameError: # We're on Python 3
pass # Do nothing
You can also use the six package, which does this and many other Python 2/3 compatibility things.
In Python 3 input() does what raw_input() does in Python 2, and Python 2's input() is gone.
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.