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.')
Related
This question already has answers here:
How to check if a float value is a whole number
(15 answers)
Closed 1 year ago.
How do I output the result of an expression where if the user enters a float number and an integer, the result will include decimal, and if the user gives both number in an integer form, the result will not include decimal.
Example:
num_1 = float(input("\nEnter first number: "))
opr = input("Enter math operator: ")
num_2 = float(input("Enter second number: "))
if opr == "+":
print(num_1 + num_2)
elif opr == "-":
print(num_1 - num_2)
else:
print("Invalid math operator")
>>> Enter first number: 2.5
>>> Enter math operator: +
>>> Enter second number: 3
5.5
The code above works for a float number that's because the input has been converted to float to avoid ValueError. But the result will always be a float number. How can I make 2 + 2 = 4 and not 4.0?
Solution 1
A simple and straightforward solution is to convert it into an int and check if it's value is same as it's float counter-part.
For example, if float_output=4.0, it's int_output would be set to 4. And the logic of 4 == 4.0 would end in True. Therefore, the int_output is set as the final_output in this case.
Else, consider that float_output=4.5, it's int_output would be set to 4. And the logic of 4 == 4.5 would end in False. Therefore, the float_output is set as the final_output.
Here is the complete code:
num_1 = float(input("Enter first number: "))
opr = input("Enter math operator: ")
num_2 = float(input("Enter second number: "))
float_output = None
if opr == "+":
float_output = num_1 + num_2
elif opr == "-":
float_output = num_1 - num_2
else:
print("Invalid math operator")
if float_output is not None:
int_output = int(float_output)
if int_output == float_output:
final_output = int_output
else:
final_output = float_output
print(final_output)
Solution 2
You can use the float.is_integer() which is an inbuilt function for floats to do this check automatically.
This is how you would do it in that case:
num_1 = float(input("Enter first number: "))
opr = input("Enter math operator: ")
num_2 = float(input("Enter second number: "))
float_output = None
if opr == "+":
float_output = num_1 + num_2
elif opr == "-":
float_output = num_1 - num_2
else:
print("Invalid math operator")
if float_output.is_integer():
print(int(float_output))
else:
print(float_output)
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))
I'm new to Python and I'm attempting to program a calculator. The problem is I can't find a way to make the variables num1 and num2 do the operation I have listed for them. All they do is concatenate the two numbers instead of performing the operation, any suggestions? Thanks.
letter = ()
class Calc():
print raw_input("What operation do you want to do?\n\tA) Addition\n\tB) Subtraction\n\ ")
num1 = raw_input("Please enter your first number: ")
num2 = raw_input("Please enter your second number: ")
if letter == 'A' or 'a':
print "The sum of", num1, "plus", num2, "equals"
print num1 + num2
elif letter == 'B' or 'b':
print "The difference of", num1, "minus", num2, "equals"
print num1 - num2
raw_input returns a string, so your two inputs are concatenated. You need to convert that input to a number before using it with numeric operators.
num1 = int(raw_input("Please enter your first number: "))
You can use either float or int to convert the input string to a number.
You also need to change
if letter == 'A' or 'a':
to
if letter == 'A' or letter == 'a':
You are using
raw_input()
which converts the input to strings.
If you want to add them together, you would like to use
num1 = float(num1)
before adding.
This is because you are doing string operations. raw_input returns a string, so you must manually convert it to an int or float using: float() or int().
Do this:
print int(num1) + int(num2) in order to print the numbers in their addition form.
I think this will do what you ask:
letter = raw_input("What operation do you want to do?\n\tA)
Addition\n\tB)Subtraction\n")
num1 = input("Please enter your first number: ")
num2 = input("Please enter your second number: ")
if letter == 'A' or 'a':
print "The sum of", num1, "plus", num2, "equals"
print num1 + num2
elif letter == 'B' or 'b':
print "The difference of", num1, "minus", num2, "equals"
print num1 - num2
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)
I'm pretty new to Python, and what makes me mad about my problem is that I feel like it's really simple.I keep getting an error in line 8. I just want this program to take the numbers the user entered and print the largest and smallest, and I want it to cancel the loop if they enter negative 1.
'int' object is not iterable is the error.
print "Welcome to The Number Input Program."
number = int(raw_input("Please enter a number: "))
while (number != int(-1)):
number = int(raw_input("Please enter a number: "))
high = max(number)
low = min(number)
print "The highest number entered was ", high, ".\n"
print "The lowest number entered was ", low, ".\n"
raw_input("\n\nPress the enter key to exit.")
The problem is that number is an int. max and min both require lists (or other iterable things) - so instead, you have to add number to a list like so:
number = int(raw_input("Please enter a number: "))
num_list = []
while (number != int(-1)):
num_list.append(number)
number = int(raw_input("Please enter a number: "))
high = max(num_list)
low = min(num_list)
Just as a note after reading dr jimbob's answer - my answer assumes that you don't want to account for -1 when finding high and low.
That's cause each time you pass one integer argument to max and min and python doesn't know what to do with it.
Ether pass at least two arguments:
least_number = min(number1, number2,...,numbern)
or an iterable:
least_number = min([number1, number2, ...,numbern])
Here's the doc
You need to change number to an list of numbers. E.g.,
print "Welcome to The Number Input Program."
numbers = []
number = int(raw_input("Please enter a number: "))
while (number != -1):
numbers.append(number)
number = int(raw_input("Please enter a number: "))
high = max(numbers)
low = min(numbers)
print "The highest number entered was ", high, ".\n"
print "The lowest number entered was ", low, ".\n"
raw_input("\n\nPress the enter key to exit.")
As mentioned by another answer, min and max can also take multiple arguments. To omit the list, you can
print "Welcome to The Number Input Program."
number = int(raw_input("Please enter a number: "))
high = low = number
while (number != int(-1)):
number = int(raw_input("Please enter a number: "))
high = max(high, number)
low = min(low, number)
print "The highest number entered was ", high, ".\n"
print "The lowest number entered was ", low, ".\n"
raw_input("\n\nPress the enter key to exit.")
num = ''
active = True
largest = 0
smallest = 0
while active:
num = input("Please enter a number")
if num == 'done':
active = False
break
else:
try:
num = int(num)
if largest < num:
largest = num
if smallest == 0 or smallest > num:
smallest = num
except ValueError:
print("Please enter a valid number")
print("Largest no. is " + str(largest))
print("Smallest no. is " + str(smallest))