I'm trying to run a code but it's not working and I can't seem to find the errors if there are any
def question3():
x= input("Enter the first value:")
y= input("Enter the second value:")
z= input("Enter the third value:")
if x==y==z:
print("All three inputs have equal values" + x)
elif x==y:
print("x and y have equal values" + x)
elif x==z:
print("x and z have equal values" + x)
elif y==z:
print("y and z have equal values" + y)
else:
print("All three inputs have different values")
def question3():
x= input("Enter the first value:")
y= input("Enter the second value:")
z= input("Enter the third value:")
if x==y==z:
print("All three inputs have equal values" + x)
elif x==y:
print("x and y have equal values" + x)
elif x==z:
print("x and z have equal values" + x)
elif y==z:
print("y and z have equal values" + y)
else:
print("All three inputs have different values")
return
Related
I edited code here to assign variables to float. It is not working.
This code is to find value of either factor in (x+y)^2 = x^2 + 2xy + y^2
There are three factors, x, y and result. Value must be available for any two.
The only problem here is, you can't find value of x or y if they are float. For that I'm looking forward in google & to use some math's library if I can understand.
x = (input("\nEnter value of x: "))
y = (input("\nEnter value of y: "))
z = (input("\nEnter value of (x+y)^2: "))
#-----------------------------------------------------------------------
#Converting input to integers.
#-----------------------------------------------------------------------
a=0.0
b=0.0
c=0.0
if len(x)!=0:
a = float(x)
else:
print ("\nWe will search value of 'x'")
if len(y)!=0:
b = float(y)
else:
print ("\nWe will search value of 'y'")
if len(z)!=0:
c = float(z)
else:
print ("\nWe will search value of '(x+y)^2'")
#-----------------------------------------------------------------------
#Calculations
#-----------------------------------------------------------------------
from math import sqrt
fv=0.0 #fv = find value
rs=0.0 #rs = result
if len(x)==0:
while True:
rs = ((fv*fv) + (2*fv*b) + (b*b))
fv = fv + 1
if rs==c:
print (f"\nValue of 'x' is either {(fv-1)} or {((sqrt(c)*-1) - b)}")
break
if len(y)==0:
while True:
rs = ((a*a) + (2*a*fv) + (fv*fv))
fv = fv+1
if rs==c:
print (f"\nValue of 'y' is either {(fv-1)} or {((sqrt(c)*-1) - a)}")
break
if len(z)==0:
rs = ((a*a) + (2*a*b) + (b*b))
print (f"\nValue of '(x+y)^2' is {rs}")
print ("\nThank you")
try this
print ("\n\nEnter values in below equations. \nIf you don't know the value, than press \"Enter\" ")
x = (input("\nEnter value of x: "))
y = (input("\nEnter value of y: "))
z = (input("\nEnter value of (x+y)^2: "))
#-----------------------------------------------------------------------
#Converting input to integers.
#-----------------------------------------------------------------------
a=0
b=0
c=0
if len(x)!=0:
a = float(x)
else:
print ("\nWe will search value of 'x'")
if len(y)!=0:
b = float(y)
else:
print ("\nWe will search value of 'y'")
if len(z)!=0:
c = int(z)
else:
print ("\nWe will search value of '(x+y)^2'")
#-----------------------------------------------------------------------
#Calculations
#-----------------------------------------------------------------------
from math import sqrt
fv=0 #fv = find value
rs=0 #rs = result
if len(x)==0:
while True:
rs = ((fv*fv) + (2*fv*b) + (b*b))
fv = fv + 1
if rs==c:
print (f"\nValue of 'x' is either {(fv-1)} or {int((sqrt(c)*-1) - b)}")
break
if len(y)==0:
while True:
rs = ((a*a) + (2*a*fv) + (fv*fv))
fv = fv+1
if rs==c:
print (f"\nValue of 'y' is either {(fv-1)} or {int((sqrt(c)*-1) - a)}")
break
if len(z)==0:
rs = ((a*a) + (2*a*b) + (b*b))
print (f"\nValue of '(x+y)^2' is {rs}")
print ("\nThank you")
just change int to float
I just started learning python. I have some experience with C++ from school. The problem is to write code that prints the largest odd number from user input and to print relevant feedback if there isn't any odd number. What is wrong with this code and are there better ways to solve this problem?
#To print the largest odd number
x = input ("Enter first number: ")
y = input ("Enter second number: ")
z = input ("Enter third number: ")
if x % 2 == 0 and y % 2 == 0 and z % 2 == 0:
print ("There are no odd numbers")
if x % 2 != 0 and x > y and x > z:
print (x, " is the largest odd number")
if y % 2 != 0 and y > x and y > z:
print (y, " is the largest odd number")
if z % 2 != 0 and z > x and z > y:
print (z, " is the largest odd number")
elif x == y == z:
print ("All the numbers have the same value")
Maybe the logic becomes easier if you make it into a small list and sort it:
x = input ("Enter first number: ")
y = input ("Enter second number: ")
z = input ("Enter third number: ")
odds = sorted([ i for i in [x,y,z] if int(i)%2 ],reverse=True)
if not odds:
print("No odd number")
elif odds.count(odds[0]) == len(odds):
print("All odd numbers are equal")
else:
print(f"{odds[0]} is the largest odd number")
2 things: 1. Convert data. 2. Your code isn't formatted correctly, by the looks of it.
For 1:
x = int(input(("Enter first number: ")) #Do this for y and z
For 2- your code will never return the largest odd number if the largest odd number is smaller than the largest even number. For example [20, 9, 5]. To fix that:
#Create a list to work with
num_li = [x,y,z]
#Get all the odd numbers
num_li = [i for i in num_li if i%2!=0]
#If no odd numbers
if len(num_li) == 0:
print('No odds')
#Print the largest odd number
else:
num_li.sort(reverse = True)
print('The largest odd number is: ' + str(num_li[0]))
There are two problems with the current version of the code:
1)TypeError - You are receiving strings as inputs and treating them like integers
2)Logical error - your conditions do not cover all cases.
I rewrote the code. strings are converted to int and all cases are covered.
Working example:
x = int(input ("Enter first number: "))
y = int(input ("Enter second number: "))
z = int(input ("Enter third number: "))
numbers = [x, y, z]
odd_numbers = []
for number in numbers: // loop through the numbers and create an odd number list
if number % 2 != 0:
odd_numbers.append(number)
if odd_numbers: //print out the largest number in the list using max()
print(max(odd_numbers))
else:
print("No odd numbers")// if the list is empty this will be printed
This question already has an answer here:
Why does this not work as an array membership test? [duplicate]
(1 answer)
Closed 4 years ago.
I'm still in the early stages of learning python so this is probably an easy question that I just can't find the answer for. I'm trying to take user input to define 2 variables and compare them using > and < in an if statement.
line 6-11 in the code below I've also tried ...is False: and also y > x is True
print("what is x")
x = int(input('> ))
print("what is y")
y = int(input('> ))
if x > y is True:
print("x > y")
elif x > y is not True:
print("y > x")
else:
print("whatever")
If x > y then it says y > x.
If y > x, it prints the else condition.
You need brackets around your x/y comparison. I modified the code and I think it works as you intended now:
print("what is x")
x = int(input('> '))
print("what is y")
y = int(input('> '))
if (x > y) is True:
print("x > y")
elif (x > y) is not True:
print("y > x")
else:
print("whatever")
EDIT: As others have pointed out in the comments, you don't have to explicitly compare the result of x > y. You can just do this:
print("what is x")
x = int(input('> '))
print("what is y")
y = int(input('> '))
if x > y
print("x > y")
elif y > x:
print("y > x")
else:
print("whatever")
I want to check whether a number is multiple of second. What's wrong with the following code?
def is_multiple(x,y):
if x!=0 & (y%x)==0 :
print("true")
else:
print("false")
end
print("A program in python")
x=input("enter a number :")
y=input("enter its multiple :")
is_multiple(x,y)
error:
TypeError: not all arguments converted during string formatting
You are using the binary AND operator &; you want the boolean AND operator here, and:
x and (y % x) == 0
Next, you want to get your inputs converted to integers:
x = int(input("enter a number :"))
y = int(input("enter its multiple :"))
You'll get a NameError for that end expression on a line, drop that altogether, Python doesn't need those.
You can test for just x; in a boolean context such as an if statement, a number is considered to be false if 0:
if x and y % x == 0:
Your function is_multiple() should probably just return a boolean; leave printing to the part of the program doing all the other input/output:
def is_multiple(x, y):
return x and (y % x) == 0
print("A program in python")
x = int(input("enter a number :"))
y = int(input("enter its multiple :"))
if is_multiple(x, y):
print("true")
else:
print("false")
That last part could simplified if using a conditional expression:
print("A program in python")
x = int(input("enter a number :"))
y = int(input("enter its multiple :"))
print("true" if is_multiple(x, y) else "false")
Some things to mention:
Conditions with and, not & (binary operator)
Convert input to numbers (for example using int()) - you might also want to catch if something other than a number is entered
This should work:
def is_multiple(x,y):
if x != 0 and y%x == 0:
print("true")
else:
print("false")
print("A program in python")
x = int(input("enter a number :"))
y = int(input("enter its multiple :"))
is_multiple(x, y)
Use and operator instead of bitwise & operator.
You need to conver values to integers using int()
def is_multiple(x,y):
if x!=0 and (y%x)==0 :
print("true")
else:
print("false")
print("A program in python")
x = int(input("enter a number :"))
y = int(input("enter its multiple :"))
is_multiple(x,y)
I tried this and worked also for when x and/or y are equal to 0. Idk if there's a shorter way of writing it.
Tested with (4,12), (12, 4), (2,0), (0,2), (0, 0)
(result should be : False True False True True).
def exo1(x,y):
#x = int(input("input number x: "))
#y = int(input("input number y: "))
if x==0 and y==0:
return True
if x>0 and y==0:
return False
if y>0 and x==0:
return True
if x!=0 and y!=0 and (x%y)==0:
return True
else:
return False
print(exo1())
print(exo1(4,12))
print(exo1(12,4))
print(exo1(2,0))
print(exo1(0,2))
print(exo1(0,0))
I'm new to programming. I have a bit of problem with python coding (with def function).
So basically the code has to give the smaller number out of 2 numbers.
Question:
Write codes to perform the following tasks:
Define a function smaller_num that takes in two numbers to
determine and return the smaller number of the two.
Ask user for two numbers
Use the function to determine the smaller number and display the result
So I had user input instead of calling the function and adding value inside the variable.
This is how my code looks like:
def smaller_num(x,y):
if x>y:
number= y
else:
number= x
return number
smaller_num(x= input("Enter first number:-") ,y= input("Enter second number:-"))
print("The smaller number between " + str(x) + " and " + str(y) + " is " + str(smaller_num))
How do I correct it? For now it's not working because "x" is not defined. But I feel that I defined them clearly both in def function and input function. So how do I fix this?
Thanks for those who respond to this question.
You never actually defined x and y globally. You only defined it in the function when you did def smaller_num(x, y).
When you do smaller_num(x= input("Enter first number:-") ,y= input("Enter second number:-"))
, you aren't creating variables called x and y, you are just creating parameters for your function.
In order to fix your code, create the variable x and y before you call your function:
def smaller_num(x, y): ## Can be rephrased to def smaller_num(x, y):
if x > y: ## if x > y:
number = y ## return y
else: ## else:
number = x ## return x
return number
x = input("Enter first number:-")
y = input("Enter second number:-")
result = smaller_num(x, y)
print("The smaller number between " + str(x) + " and " + str(y) + " is " + str(result))
The other reason your code is not working is because you're not assigning the returned value of the function back into a variable. When you return something from a function, and again when you call the function, you need to assign the value to a variable, like I have: result = smaller_num(x, y).
When you called your function, you never assigned the value to a variable, so it has been wasted.
Also, are you using Python 3 or 2.7? In python 3 using input() will return a string, and to convert this to an integer, you can call int() around the input() function.
This will work:
def smaller_num(x,y):
if x>y:
number= y
else:
number= x
return number
x = input("Enter first number:-")
y = input("Enter second number:-")
smaller = smaller_num(x,y)
print("The smaller number between " + str(x) + " and " + str(y) + " is " + str(smaller))
this should work:
def smaller_num(x,y):
if x>y:
number = y
else:
number = x
return number
x = input("Enter first number:-")
y = input("Enter second number:-")
print("The smaller number between " + str(x) + " and " + str(y) + " is " + str(smaller_num(x,y)))
def smaller_num(x,y):
if x>=y:
number = y
else:
number = x
return number
x = input("Enter first number:-")
y = input("Enter second number:-")
print("The smaller number between " + str(x) + " and " + str(y) + " is " + str(smaller_num(x,y)))
def smaller_num(x,y):
if x>y:
number= y
else:
number= x
return number
k=smaller_num(x= input("Enter first number:-") ,y= input("Enter second number:-"))
print("The smaller number between x & y is ", k)
def smaller_num():
x=int(input('Enter the first number: '))
y=int(input('Enter the second number: '))
if x<y:
return x
else:
return y
print('The smaller number is: ',smaller_num())