When I run the code below, I get the following error: eval() arg 1 must be a string or code object
Anyone know why? This is code i'm learning from a book, so I assumed that it would be correct.
# Prompt the user to enter three numbers
number1 = eval(input("Enter the first number: "))
number2 = eval(input("Enter the second number: "))
number3 = eval(input("Enter the third number: "))
# Compute average
average = (number1 + number2 + number3) / 3
print("The average of", number1, number2, number3, "is", average)
You are using input() on Python 2, which already runs eval() on the input. Just remove the eval() call, or replace input() with raw_input().
Alternatively, use Python 3 to run this code, it is clearly aimed at that version. If your book uses this syntax, then you want to use the right version to run the code samples.
Most of all, don't use input() on Python 2 or eval() on Python 3. If you want integer numbers, use int() instead.
Python 2 example:
# Prompt the user to enter three numbers
number1 = int(raw_input("Enter the first number: "))
number2 = int(raw_input("Enter the second number: "))
number3 = int(raw_input("Enter the third number: "))
# Compute average
average = (number1 + number2 + number3) / 3
print "The average of", number1, number2, number3, "is", average
Python 3 version:
# Prompt the user to enter three numbers
number1 = int(input("Enter the first number: "))
number2 = int(input("Enter the second number: "))
number3 = int(input("Enter the third number: "))
# Compute average
average = (number1 + number2 + number3) / 3
print("The average of", number1, number2, number3, "is", average)
Related
This question already has answers here:
return statement in for loops [duplicate]
(6 answers)
Closed 1 year ago.
The goal is for the computer to guess a number given an upper and lower bound, the computer should take no as an input and run the program again until a yes is given.
This is the code I am using right now, but the loop only ever runs once dues to where the return function is. If I take that out and jump directly to print it runs continuously.
import random
num1 = int(input("enter your minumum value: "))
num2 = int(input("enter your maximum value: "))
def number_choice(num1,num2):
guess = "no"
while guess == "no":
return random.choice(range(num1,num2))
print (number_choice(num1,num2))
guess = input("is that your number, enter yes or no: ")
Try this:
import random
num1 = int(input("enter your minimum value: "))
num2 = int(input("enter your maximum value: "))
def number_choice(num1,num2):
print(random.choice(range(num1,num2)))
guess='no'
while guess.lower()=='no':
number_choice(num1,num2)
guess = input("Is that your number, enter yes or no: ")
print('Cheers!')
or you can use this too:
import random
num1 = int(input("enter your minimum value: "))
num2 = int(input("enter your maximum value: "))
def number_choice(num1,num2):
print(rnd.choice(range(num1,num2)))
guess=input('Is this your number? Type yes or no ')
if guess=='no':
number_choice(num1,num2)
else:
print('cheers!')
number_choice(num1,num2)
Hope it helps!
first_num, second_num = input("Enter the first number: "), input("Enter the second number: ")
if first_num > second_num :
print(first_num, ' is the greatest number.')
else :
print(second_num, ' is the greatest number.')
You are comparing two strings, not integer or float.
you must convert input to int or any other format you want then compare them. So the complete code should be:
first_num, second_num = int(input("Enter the first number: ")),
int(input("Enter the second number: "))
if first_num > second_num :
print(first_num, ' is the greatest number.')
else :
print(second_num, ' is the greatest number.')
The code is:Take 2 numbers from the user. Pass these two numbers to a function mathExp(). This function will calculate the sum of the cubes of the two numbers.
Return and print this value.
Function Structure:
int mathExp(int,int)
My code is:
num1 = int(input("write a number: "))
num2 = int(input("write a number: "))
def mathExp(num1,num2):
print(num**3 + num**2, mathExp(num1,num2))
There is a lot wrong with this code:
First, you are calling the mathExp function IN itself. Thats recursion and I dont think that you want to do this.
Second, the parameters of the mathExp function are called num1 and num2. But in the function you use just num which doesnt exist.
So what you probably want to do is this:
num1 = int(input("write a number: "))
num2 = int(input("write a number: "))
def mathExp(n1,n2):
return n1**3 + n2**3
print(mathExp(num1, num2))
Your mathExp function was wrong. Try this instead:
num1 = int(input("write a number: "))
num2 = int(input("write a number: "))
def mathExp(num1,num2):
ans = num1**3 + num2**3
return ans
cubes = mathExp(num1, num2)
print(cubes)
Try this code:
# take two number as input from the user
num1 = int(input("write a number: "))
num2 = int(input("write a number: "))
# define the function which calculate the sum of cubes of the input numbers
def mathExp(num1,num2):
result = num1**3 + num2**3
return result
# call the function with user input and print the result
print(mathExp(num1, num2))
Example:
write a number: 4
write a number: 7
407
I am trying to write a program which asks an input of two numbers and then prints the sum, product and average by running it. I wrote a program but it asks an input for 2 numbers everytime i need a sum or average or product. How can I get all 3 at once, just making two inputs once.
sum = int(input("Please enter the first Value: ")) + \
int(input("Please enter another number: "))
print("sum = {}".format(sum))
product = int(input("Please enter the first Value: ")) * \
int(input("Please enter another number: "))
print ("product = {}".format(product))
Use variables to store the input:
first_number = int(input("Please enter the first Value: "))
second_number = int(input("Please enter another number: "))
sum = first_number + second_number
product = first_number * second_number
average = (first_number + second_number) / 2
print('Sum is {}'.format(sum))
print('product is {}'.format(product))
print('average is {}'.format(average))
You need to assign your numbers to variables and then reuse them for the operations.
Example
x = int(input("Please enter the first Value: "))
y = int(input("Please enter another number: "))
print("sum = {}".format(x+y))
print("product = {}".format(x*y))
print("average = {}".format((x+y)/2))
You're going to want to get the numbers first, then do your operation on them. Otherwise you're relying on the user to alway input the same two numbers:
a = int(input("Please enter the first Value: "))
b = int(input("Please enter the second Value: "))
print ("sum = {}".format(a+b))
print ("product = {}".format(a*b))
print ("average = {}".format((a*b)/2))
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
number1 = float
number2 = float
number1 = raw_input("Please input the first number: ")
number2 = raw_input("Please input the second number: ")
if number1 > number2:
print number1 + ' is bigger than ' + number2
elif number2 < number1:
print number2 + ' is bigger than ' + number1
else:
print 'You did not follow the instructions properly. Goodbye!'
print "\n"
raw_input("Please press enter to exit.")
These lines:
number1 = float
number2 = float
do not make the inputs into floats. Instead, all they do is assign the variables to the built-in float.
This is what you should be doing:
number1 = float(raw_input("Please input the first number: "))
number2 = float(raw_input("Please input the second number: "))
Also, inside your if-statements, you cannot add strings and floats together (trying to do so will raise a TypeError). There are two ways to solve this problem. The first is with str.format:
print '{} is bigger than {}'.format(number1, number2)
The second is to separate the values with commas:
print number1, 'is bigger than', number2
Finally, your logic is a little off. The second if-statement should be like this:
elif number1 < number2:
Otherwise, it does the same thing as the first if-statement.
Below is a fixed version of your script:
number1 = float(raw_input("Please input the first number: "))
number2 = float(raw_input("Please input the second number: "))
if number1 > number2:
print '{} is bigger than {}'.format(number1, number2)
elif number1 < number2:
print '{} is bigger than {}'.format(number2, number1)
else:
print 'You did not follow the instructions properly. Goodbye!'
print "\n"
raw_input("Please press enter to exit.")
You are comparing strings, which means they are compared lexicographically.
Cast the return value of raw_input to float:
number1 = float(raw_input("Please input the first number: "))
number2 = float(raw_input("Please input the second number: "))
The lines:
number1 = float
number2 = float
merely store references to the float() constructor. Python doesn't have type declarations, and those lines do not mean that the two names should only hold floating point values.
You do then need to turn the floating point numbers back to strings when printing; you could use the fact that print accepts multiple values to have this done for you:
if number1 > number2:
print number1, 'is bigger than', number2
elif number2 < number1:
print number2, 'is bigger than', number1
Alternatively, you could store the raw_input() results as strings and only turn the values to float() when comparing:
number1 = raw_input("Please input the first number: ")
number2 = raw_input("Please input the second number: ")
if float(number1) > float(number2):
print number1 + ' is bigger than ' + number2
elif float(number2) < float(number1):
print number2 + ' is bigger than ' + number1