This question already has answers here:
What is the purpose of the return statement? How is it different from printing?
(15 answers)
Closed 5 years ago.
I'm practicing Python so I decided to recreate the max() function for 2 numbers. The code doesn't have an error, it just doesn't return anything. Please help! `
def newMax(num1, num2):
if num1 > num2:
return num1
elif num2 > num1:
return num2
elif num1 == num2:
return "They're both equal!"
else:
return "We've run into some sort of error. Make sure you entered 2 numbers."
print("This program will return the largest of 2 numbers you enter.")
number1 = input("Please enter your first number.")
number2 = input("Please enter your second number.")
newMax(number1, number2)
`
Can you not call a function with variables as the parameters, and if not then how would I write this program? FIGURED OUT, I had a print statement error, sorry.
new_max = newMax(number1, number2)
print(new_max)
Try assigning it to a variable and printing that variable.
Related
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 6 months ago.
The community is reviewing whether to reopen this question as of 5 months ago.
Improve this question
I just want to use input, in a function in python.
this is my code:
print("I can tell you the maximum of 3 numbers")
def max_num(num1, num2, num3, false=None):
num1 = input("enter first number")
num2 = input("enter second number")
num3 = input("enter third number")
if num1 >= num2 and num1 >= num3:
return num1
elif num2 >= num1 and num2 >= num3:
return num2
elif num1.isdigit(False) and num2.isdigit(False) and num3.isdigit(False):
print("no number available")
else:
return num3
return max_num()
but when I run this code, Just first line (print), runs succesfully.
what is wrong?
I would be thankful.
When defining a function with parameters, make sure that these parameters don't come inside the definition of the function.
The code also has some indentation and logical mistakes.
This is a corrected version.
print("I can tell you the maximum of 3 numbers")
num1 = input("Enter the first number:")
num2 = input("Enter the second number:")
num3 = input("Enter the third number:")
def max_num(num1, num2, num3):
if not num1.isdigit() or not num2.isdigit() or not num3.isdigit():
return "Wrong Input"
else:
num1,num2,num3=int(num1),int(num2),int(num3)
if num1 >= num2 and num1 >= num3:
return num1
elif num2 >= num1 and num2 >= num3:
return num2
else:
return num3
print(max_num(num1,num2,num3))
You need to call that function like print(max_num(num1, num2, num3)), you are still create the function but not calling it.
What does return max_num() even mean? You should return a number, and a number is already returned in all possible branches.
If you want to call this function, try something like print(max_num(1, 2, 3)) at the end of the script.
Also, it is a bit confusing: are you planning to pass in the three numbers by functions arguments or user input? Because you are attempting to do both right now.
Ok, there are lots of issues with this code, but to answer your original question "Why does input never get called?", the answer is simple:
You have defined the function max_num, but you have never called it. Only once a function is called does the code inside run.
So in your script, you simply need to remove all the input parameters in your function definition (as they are never used), and add the line:
max_num()
Then you'll need to fix all the other quirks :)
Happy coding
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'm trying to create a simple program using function and if else. Even though there is no errors, my program doesn't give expected result
value1 = float(input("Enter your first number:"))
value2 = float(input("Enter your second number:"))
question= input("choose mode:" )
add = True
def nor_cal(num1, num2):
if question == add:
total = num1+num2
print("the total is: ")
print(total)
else:
print("Invalid operator")
result1 = nor_cal(value1, value2)
print(result1)
when i run the program, it shows like this:
Enter your first number:2
Enter your second number:3
choose mode:add
Invalid operator
None
I don't know where i'm wrong please help !
The bug is in the line if question == add:, what you're asking the program to do is to compare the variable question (which is "add") to the variable add (which is True).
You're going to want to use if question == "add": instead.
This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 1 year ago.
I wanted to do the following simple calculation by passing values for the parameters num1 and num2 from input() methods.
I tried following code:
def add(num1, num2):
return num1 * num2
num1 = input('Enter number1: ')
num2 = input('Enter number2: ')
print(add(num1, num2))
But it is showing the following error when it is run (After input num1 and num2):
TypeError: can't multiply sequence by non-int of type 'str'
Can somebody please explain where did I go wrong and how to convert an input string to the integer type?
input() will returns a string, you have to convert the string to a int or float, you can test if the input is a valid number.
isnumeric() will return true if all chars in the string are numeric ones and the string is not empty.
Note: i renamed the function to multiply because this is not adding ...
def multiply(num1,num2):
return num1*num2
inp1=input('Enter number1: ')
inp2=input('Enter number2: ')
if inp1.isnumeric() and inp2.isnumeric():
num1 = int(inp1)
num2 = int(inp2)
print(multiply(num1,num2))
else:
print("Atleast one input is not numeric")
You can try this:
def add(num1, num2):
return num1 * num2
num1 = int(input('Enter number1: '))
num2 = int(input('Enter number2: '))
print(add(num1, num2))
The input function stores the input as a string and so what is happening in your code is that you are entering two integers but they are being stored as strings. You cannot multiply two strings. All you need to do is convert the input strings to integers by using the [int()][2] function. If you want to multiply floats, you can use the [float()][3] function in place of the int() function. You can also convert the strings to integers or floats once you have passed them into the function. Something like this:
def add(num1, num2):
return int(num1) * int(num2)
num1 = input('Enter number1: ')
num2 = input('Enter number2: ')
print(add(num1, num2))
input (in Python 3) returns a str which is seen as text and not a number. If you want to multiply (or add) numbers, you must first parse the strings as numbers.
Sometimes this will fail as well. If you're assuming the input is always going to be a valid number, you can convert a string using:
float("3.1415926")
You can use this in your code with:
def add(num1,num2):
return num1*num2
num1=float(input('Enter number1: '))
num2=float(input('Enter number2: '))
print(add(num1,num2))
To avoid floating point errors, you can print/display the float using an f-string (added in Python 3.6).
def add(num1,num2):
return num1*num2
num1=float(input('Enter number1: '))
num2=float(input('Enter number2: '))
print(f"{add(num1,num2):.2f}")
def mul(num1,num2):
return(num1*num2)
inp1 = input('Enter number1:')
inp2 = input('Enter number2:')
num1 = int(inp1)
num2 = int(inp2)
print(mul(num1,num2))
This question already has answers here:
How do I determine if the user input is odd or even?
(4 answers)
Closed 4 years ago.
I tried to make a odd/even 'calculator' in python and it keeps popping up errors. Here's the code:
def odd_even():
print("Welcome to Odd/Even")
num = input("Pick a number: ")
num2 = num/2
if num2 == int:
print("This number is even")
else:
print("This number is odd")
Id like to know whats causing the errors and solutions to them
There is an error in the line: num = input("Pick a number: ")
Because input method always returns a String,so you should convert it into int to performs the integer operation
The currect code is:
num =int( input("Pick a number: "))
you can't do math with strings convert it to int
try:
num = int(input("Pick a number: "))
except ValueError:
print('This is not a number!')
return
This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 5 years ago.
I was hoping on some input concerning the use of 'user inputted' arguments as variable value amounts to be used in a calculation within a function... this will no doubt be a very simple issue but I am new to python and so far my attempts at fixing it have been flawed.
The function is a simple calculation of adding two numbers together solely for the purpose of introducing new students to functions.
I have no issues with the function working as intended when I feed hard coded values as integers into the parameters (which are included in this example code)
but when I try to pass 'user input' argument values in when actually running the program the function simply concatenates the two values rather than adding them, this is (I presume) because the input values are in the format of a 'string'.
I have tried introducing 'int' statements after the 'input' statements within the argument variable code but this results in an error 'invalid literal for int()' I have also tried this at different points of the function code itself with no luck..... so how would I go about making sure the values are recognised as integers before or during calculation? Thanks
def getSum(num1, num2):
calc = num1 + num2
return calc
num1 = input("Type in your first number to add: ")
num2 = input("Type in your second number to add: ")
result1 = getSum(num1, num2)
answer = getSum(10, 5)
answer2 = getSum(155, 56668)
print(answer)
print(answer2)
print(result1)
int() should work correctly if the user only enters integer values - otherwise you have to wrap it with a try and catch block
try:
num1 = int(input("Type in your first number to add: "))
num2 = int(input("Type in your second number to add: "))
catch Exception as ex:
pass //do nothing
Just introduce int() for num1 and num2 in line2.
So the new code will be:
def getSum(num1, num2):
calc = int(num1) + int(num2)
return calc
num1 = input("Type in your first number to add: ")
num2 = input("Type in your second number to add: ")
result1 = getSum(num1, num2)
answer = getSum(10, 5)
answer2 = getSum(155, 56668)
print(answer)
print(answer2)
print(result1)