If statement error. Comparison of numbers to output max - python

I'm writing a simple code on python, but have come across an unusual error. The statements are to compare thre numbers and return the max value. Most of the time the program executes fine, however, if for example num3 is a 3-digit number and others are not, I get a comparison error. Any ideas?
def maxNum(num1, num2, num3):
if num1 >= num2 and num1 >= num3:
return num1
elif num2 >= num3 and num2 >= num1:
print (num2)
return num2
else:
print (num3)
return num3
num1 = input("Please enter first number: ")
num2 = input("Please enter second number: ")
num3 = input("Please enter third number: ")
print ("Max number is:", maxNum(num1, num2, num3))
Please click on image link. I have changed the code as advised but still get error. It seems any integer that I input that is more than or equal to 100 creates this error:
enter image description here

1 def max_num(num1, num2, num3):
2 biggest = max([int(num1),int(num2), int(num3)])
3 return biggest
4
5 num1 = input("Enter First Number: ")
6 num2 = input("Enter Second Number: ")
7 num3 = input("Enter Third Number: ")
8
9
10 print(max_num(num1, num2, num3))
Are you just trying to work the function comparisons, or are you open to other methods?
This will accomplish the same task with less steps.

Although I am by no means fluent in Python, I believe the approach to distinguish all cases that can occur is hard to understand. Instead, I suggest iterating the input values as follows. The approach uses an additional varaiable to store the potential result.
def maxNum(num1, num2, num3):
result = num1
if (num2 > result)
result = num2
if (num3 > result)
result = num3
print(result)
return result

Related

how do i turn an input into a variable so i can add or use in equations in python

if I have a code like this?
num1= input (" number goes here")
num2= input ("number goes here")
how can I make a simple equation work such as.
num3=num2+num1
print ("num3")
and when I do that it outputs
num2num3
I have tried things such as
int=(num1)
You need to convert num1 and num2 into int type because input gives you a str type.
num1 = int(input(" number goes here"))
num2 = int(input("number goes here"))
num3 = num1 + num2
print(num3)

Where did i wrong?Take 2 integers, pass function and calcualte sum of cubes

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

Trying to make a pow() calculator, without the pow, ERROR message "'int' object has no attribute 'count'"

The idea is, that the calculator is supposed to detect the numbers of num1 in the calculation and add more until it is equal to the num2, as it is supposed to be num1 raised to the power of num2.
But I keep receiving this message "'int' object has no attribute 'count'". I know the count is for lists, so I was wondering, what the appropriate command would be for int (or float).
Furthermore, how would I go about adding the numbers of "num1" to the equation?
Here is the whole code
num1 = int(input("Enter a number"))
operator = input("Now enter an operator")
num2 = int(input("Enter a new number"))
Pow = (num1*num1)
if operator == "^":
print(Pow)
while Pow.count(num1) < num2 += 1:
Pow = num1 * num1
num1 = int(input("Enter a number: "))
operator = input("Now enter an operator: ")
num2 = int(input("Enter a new number: "))
goal = num1 ** num2
#print("goal: " + str(goal))
count = 1
if operator == '^':
while num1 < goal:
#print(num1)
num1 += num1
count += 1
#print(num1)
print ("It took %(count)d loops to get to %(num)d!" % {'count': count, 'num': goal})
Your question is vague, but I believe this is what you are looking for?
Edited code above and included example below
You have an Invalid Syntax here which is while Pow.count(num1) < num2 += 1:
Have a look at this edited code:
num1 = int(input("Enter a number"))
operator = input("Now enter an operator")
num2 = int(input("Enter a new number"))
Pow = (num1*num1)
if operator == "^":
print(Pow)
while num1 < num2:
Pow = num1 * num1
num2 += 1

If statement not evaluating all the conditions and executing anyway [duplicate]

This question already has answers here:
How to test multiple variables for equality against a single value?
(31 answers)
Closed 6 years ago.
Ok so I am new to python and trying to learn how to code. I ran into an issue today that I don't understand. So this code executes as expected and prints the largest of the three numbers no matter what position the largest number is.
if num1 >= num2 and num3:
print(num1, 'Is the greatest number!')
elif num2 >= num3 and num1:
print(num2, 'Is the greatest number!')
else:
print(num3, 'Is the greatest number')
But if i change the elif statement to this:
elif num2 >= num1 and num3:
print(num2, 'Is the greatest number!')
Even if num3 is the largest the else statement will not execute and it will display the larger number of num1 or num2.
Your first version works purely by coincidence. You need to do
if num1 >= num2 and num1 >= num3:
print(num1, 'Is the greatest number!')
elif num2 >= num3 and num2 >= num1:
print(num2, 'Is the greatest number!')
else:
print(num3, 'Is the greatest number')
Although, this will still print wrong info if any of the numbers are equal
The problem here is a misunderstanding in the way the keyword and works.
In python, and is used to separate two complete logical statements: cond1 and cond2. It first checks cond1. If cond1 is True, it moves on to check cond2. If cond2 is also True, then the whole statement evaluates to True.
When you do if num1 >= num2 and num3, you are actually asking python if the following is True:
num1 >= num2
num3 exists and is not 0 (since it's a number)
It is not checking if num1 >= num2 and num1 >= num3.
So if num1 = 2, num2 = 1, num3 = 3, your conditional will still return True for if num1 >= num2 and num3.
The same concept applies to your problem conditional.

How do I print a float as an integer?

This is for a homework assignment in my Python class that I am having an issue on. I am not allowed to import anything or use try-except.
I want to take 3 numbers and print the smallest one without using the min() function. The issue I am having is that if the number is something like 5 it gets printed as 5.000000 because I converted all the numbers to floats. I tried using rstrip('0') and it prints it as 5. which makes sense, but I need integers to be printed as an integer and floats to be printed as a float. Is there a simpler way to do this that I am missing?
My code for reference:
def min3(num1, num2, num3):
if (num1 <= num2):
if(num1 <= num3):
return num1
else:
return num3
if (num2 <= num1):
if(num2 <= num3):
return num2
else:
return num3
def main():
num1 = float(input("Please enter the first number: "))
num2 = float(input("Please enter the second number: "))
num3 = float(input("Please enter the third number: "))
smallest_num = min3(num1, num2, num3)
print("The smallest number is %f." %(smallest_num))
main()
You can do something like this
if smallest_num.is_integer():
# print as integer
print "The smallest is %d" % smallest_num
else:
# print as float
print "The smallest is %f" % smallest_num

Categories