Python 3 - Why wont my simple program do anything with the input? - python

i just started learning Python 3. So i learned some basics and tried writing something on my own. Its a little calc, but after the user does its inputs nothing happens, it justs ends. Sorry if this is a very stupid Question. Thanks in advance
print("Welcome")
n1 = float(input("Please insert a number"))
o1 = input("Please insert the operator(+,-,*,/)")
n2 = float(input("Please insert another number"))
def mult(x, y):
z = x * y
return z
def addi(x, y):
z = x + y
return z
def subi(x, y):
z = x - y
return z
def divi(x, y):
if x == 0 or y == 0:
print("Cant divide 0")
elif x == 0 and y == 0:
print("Cant divide 0")
else:
z = x / y
return z
if o1 == "+":
addi(n1, n2)
elif o1 == "-":
subi(n1, n2)
elif o1 == "*":
mult(n1, n2)
elif o1 == "/":
divi(n1, n2)
else:
print("Wrong Operator!")
EDIT: Thanks to all of you, I fixed it and it works. Thanks again.

You're doing the calculations, but not outputting them :
if o1 == "+":
print(addi(n1, n2))
elif o1 == "-":
print(subi(n1, n2))
elif o1 == "*":
print(mult(n1, n2))
elif o1 == "/":
print(divi(n1, n2))
else:
print("Wrong Operator!")
NOTE:
There is a mistake in your divi function, use this instead :
def divi(x, y):
if y == 0:
print("Cant divide by 0")
else:
z = x / y
return z
+ You can just return the result without storing it in a variable :
def mult(x, y):
return x * y
def addi(x, y):
return x + y
def subi(x, y):
return x - y
def divi(x, y):
if y == 0:
print("Cant divide by 0")
else:
return x / y

It works perfectly may be you just want to print the result
Add print
print("Welcome")
n1 = float(input("Please insert a number"))
o1 = input("Please insert the operator(+,-,*,/)")
n2 = float(input("Please insert another number"))
def mult(x, y):
z = x * y
return z
def addi(x, y):
z = x + y
return z
def subi(x, y):
z = x - y
return z
def divi(x, y):
if x == 0 or y == 0:
print("Cant divide 0")
elif x == 0 and y == 0:
print("Cant divide 0")
else:
z = x / y
return z
if o1 == "+":
print(addi(n1, n2))
elif o1 == "-":
print(subi(n1, n2))
elif o1 == "*":
print(mult(n1, n2))
elif o1 == "/":
print(divi(n1, n2))
else:
print("Wrong Operator!")
Output
Welcome
Please insert a number34
Please insert the operator(+,-,*,/)+
Please insert another number23
57.0

You calculates operation results, but use it anyway - so it not changes program state (e.g. not writing in standart output).
See next:
if o1 == "+":
addi(n1, n2) # addi return value not used
elif o1 == "-":
subi(n1, n2) # subi return value not used
elif o1 == "*":
mult(n1, n2) # mult return value not used
elif o1 == "/":
divi(n1, n2) # divi return value not used
else:
print("Wrong Operator!")
As you can see - return values not used.
Let's see functions
def mult(x, y):
z = x * y
return z
def addi(x, y):
z = x + y
return z
def subi(x, y):
z = x - y
return z
def divi(x, y):
if x == 0 or y == 0:
print("Cant divide 0")
elif x == 0 and y == 0:
print("Cant divide 0")
else:
z = x / y
return z
It's just return values, but not output it anyway.
So you need to print return values. E.g. -
if o1 == "+":
print(addi(n1, n2))
elif o1 == "-":
print(subi(n1, n2))
elif o1 == "*":
print(mult(n1, n2))
elif o1 == "/":
print(divi(n1, n2))
else:
print("Wrong Operator!")

Related

how to make a guess number game?

im trying to make a guessing game, user can input the level she wants, if 1 just numbers between 1,9 and if 2 between 10,99 otherwise between 100,99... here is my code:
fals = 0
while fals < 10:
if level == 1:
x = random.randint(0,9)
y = random.randint(0,9)
elif level == 2:
x = random.randint(10,99)
y = random.randint(10,99)
else: # Next convert to else
x = random.randint(100,999)
y = random.randint(100,999)
answer = x + y
while (guess := input(f'{x} + {y} = ')) != answer:
fals += 1
if fals == 3:
print(f'{x} + {y} = {answer}')
break
problem is when i input 1 or 2 or 3 it always returns a 3digit question like 395 + 341
--- anyway the other problem is i want to ask 10 questions and also for each question if user inputs the answer 3 time false, program will give the user correct answer and keep asking other question
import random
fals = 1
while fals <= 10:
level = int(input(str(fals)+". Enter level :"))
if level == 1:
x = random.randint(0,9)
y = random.randint(0,9)
elif level == 2:
x = random.randint(10,99)
y = random.randint(10,99)
else: # Next convert to else
x = random.randint(100,999)
y = random.randint(100,999)
answer = x + y
i = 0
while i < 3:
guess = input(f'{x} + {y} = ')
if int(guess) == answer:
print("Correct")
break
i += 1
if i == 3:
print("Answer ",answer)
fals+=1
Hope this code works.

The result is not what I expected, python console problem

new to coding, when enter an equation of 1 * 5, I get "You are ...lazy". But I need to get "You are ...very lazy". Can you help me finding the problem?
Expected:
You are ... lazy ... very lazy
5.0
Do you want to store the result? (y / n):
Found:
You are ... lazy
5.0
Do you want to store the result? (y / n):
msg_0 = "Enter an equation"
msg_1 = "Do you even know what numbers are? Stay focused!"
msg_2 = "Yes ... an interesting math operation. You've slept through all classes, haven't you?"
msg_3 = "Yeah... division by zero. Smart move..."
msg_4 = "Do you want to store the result? (y / n):"
msg_5 = "Do you want to continue calculations? (y / n):"
msg_6 = " ... lazy"
msg_7 = " ... very lazy"
msg_8 = " ... very, very lazy"
msg_9 = "You are"
memory = 0
def is_one_digit(v):
v = float(v)
if -10 < v < 10 and v.is_integer():
return True
else:
return False
def check(v1, v2, v3):
msg = ""
if is_one_digit(v1) and is_one_digit(v2):
msg = msg + msg_6
if (v1 == 1 or v2 == 1) and v3 == "*":
msg = msg + msg_7
if (v1 == 0 or v2 == 0) and (v3 == "*" or v3 == "+" or v3 == "-"):
msg = msg + msg_8
if msg != "":
msg = msg_9 + msg
print(msg)
while True:
calc = input(msg_0)
try:
x = calc.split()[0]
oper = calc.split()[1]
y = calc.split()[2]
if x == "M":
x = memory
if y == "M":
y = memory
float(x)
float(y)
if oper in ["+", "-", "*", "/"]:
check(x, y, oper)
if oper == "+":
result = float(x) + float(y)
print(result)
elif oper == "-":
result = float(x) - float(y)
print(result)
elif oper == "*":
result = float(x) * float(y)
print(result)
elif oper == "/":
if float(y) != 0:
result = float(x) / float(y)
print(result)
else:
print(msg_3)
continue
user_input = input(msg_4)
if user_input == "y":
memory = result
user_i = input(msg_5)
if user_i == "y":
continue
elif user_i == "n":
break
else:
user_i = input(msg_5)
elif user_input == "n":
user_i = input(msg_5)
if user_i == "y":
continue
elif user_i == "n":
break
else:
user_i = input(msg_5)
else:
user_input = input(msg_5)
else:
print(msg_2)
except ValueError:
print(msg_1)
continue
You call float(x) and float(y), but this is not saved anywhere, so v1 and v2 will be '1' and '5', instead of 1 and 5.
If you set
x = float(x)
y = float(y)
, it should work.

updating a variable using using the output from another variable

I am trying to add the value of "next_answer" to current_answer anytime a new calculation is done but when I run the code the next_answer doesn't get added to the current_answer.
Did I position the "current_answer += next_answer" wrongly? Please help me:)
#calculator
#Add
def add(n1, n2):
return n1 + n2
#subtract
def subtract(n1, n2):
return n1 - n2
#multiply
def multiply(n1, n2):
return n1 * n2
#divide
def divide(n1, n2):
return n1 / n2
operations = {
"+": add,
"-": subtract,
"*":multiply,
"/":divide,
}
num1 = int(input("What is the first number?: "))
for operation in operations:
print(operation)
select_operator = input("Which operator do you want? Type +, - * or /")
num2 = int(input("What is the next number?: "))
operation_function = operations[select_operator]
answer = operation_function(num1, num2)
print(f"{num1} {select_operator} {num2} = {answer}")
continue_ = True
while continue_:
current_answer = answer
next_calculation = input(f"Type 'y' to continue calculating with
{current_answer} or type 'n' to exit.:")
if next_calculation == "y":
select_operator = input("Which operator do you want? Type +, - * or /")
next_number = int(input("What is the next number?:"))
operation_function = operations[select_operator]
next_answer = int(operation_function(current_answer, next_number))
print(f"{current_answer} {select_operator} {next_number} = {next_answer}")
current_answer += next_answer
elif next_calculation == "n":
continue_ = False
print("Thanks for using my calculator")
Note that every time you go through the while loop, you set current_answer to be answer, which is the first answer. So you are overriding your update to the first calculation.
See line current_answer = answer.
Some of the content has been simplified, so refer to it as appropriate:
operations = {"+": lambda x, y: x + y, "-": lambda x, y: x - y, "*": lambda x, y: x * y, "/": lambda x, y: x / y}
select_operator = input("Which operator do you want? Type: +, - * or /:")
num1 = int(input("What is the first number?: "))
num2 = int(input("What is the next number?: "))
answer = operations[select_operator](num1, num2)
print(f"{num1} {select_operator} {num2} = {answer}\n")
while True:
next_calculation = input(f"Type 'y' to continue calculating with {answer} or type 'n' to exit.:")
if next_calculation == "y":
preview_answer = answer
select_operator = input("Which operator do you want? Type: +, - * or /:")
next_number = int(input("What is the next number?:"))
answer = operations[select_operator](answer, next_number)
print(f"{preview_answer} {select_operator} {next_number} = {answer}")
else:
print("Thanks for using my calculator")
exit()
print()

Python: How can this code be made better?

WAP to create math application using function concept add minimum 20-25 functionalities different function for each functionalities and user input for selection of any function based on the functionality ask user for the input.
import math
def add(x, y):
return x + y
def subtract(x, y):
return x - y
def multiply(x, y):
return x * y
def divide(x, y):
return x / y
def floor(x , y):
return x // y
def exponent(x, y):
return x ** y
def remainder(x, y):
return x % y
def square(x, y):
a = x ** 0.5
b = y ** 0.5
return a,b
def swap(x, y):
temp = x
x = y
y = temp
return int(x), int(y)
def factors(x):
print("The factors of",x,"are:")
for i in range(1, x + 1):
if x % i == 0:
print(i)
def lcm(x, y):
if x > y:
greater = x
else:
greater = y
while(True):
if((greater % x == 0) and (greater % y == 0)):
lcm = greater
break
greater += 1
return lcm
def hcf(x, y):
if x > y:
smaller = y
else:
smaller = x
for i in range(1, smaller+1):
if((x % i == 0) and (y % i == 0)):
hcf = i
return hcf
def odd_even(x):
if (x % 2) == 0:
print("{0} is Even".format(x))
else:
print("{0} is Odd".format(x))
def factorial(x):
return math.factorial(x)
def absolute(x):
return math.fabs(x)
def power(x, y):
return math.pow(x, y)
def sin(x):
return math.sin(x)
def cos(x):
return math.cos(x)
def tan(x):
return math.tan(x)
def radian(x):
return math.radians(x)
print("Select operation.")
print("1.Add")
print("2.Subtract")
print("3.Multiply")
print("4.Divide")
print("5.Floor")
print("6.Exponent")
print("7.Remainder")
print("8.Square")
print("9.Swap")
print("10.Factors")
print("11.LCM")
print("12.HCF")
print("13.Odd or Even")
print("14.Factorial")
print("15.Absolute")
print("16.Power")
print("17.Sin")
print("18.Cos")
print("19.Tan")
print("20.Radian")
choice = input("Enter choice(1-20): ")
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
if choice == '1':
print(num1,"+",num2,"=", add(num1,num2))
elif choice == '2':
print(num1,"-",num2,"=", subtract(num1,num2))
elif choice == '3':
print(num1,"*",num2,"=", multiply(num1,num2))
elif choice == '4':
print(num1,"/",num2,"=", divide(num1,num2))
elif choice == '5':
print("Floor:", floor(num1,num2))
elif choice == '6':
print(num1,"** ",num2,"=", exponent(num1,num2))
elif choice == '7':
print(num1," % ",num2,"=", remainder(num1,num2))
elif choice == '8':
print("Square:", square(num1,num2))
elif choice == '9':
print("Swap:", swap(num1,num2))
elif choice == '10':
print(factors(num1))
elif choice == '11':
print("LCM is:", lcm(num1,num2))
elif choice == '12':
print("HCF is:", hcf(num1,num2))
elif choice == '13':
print(odd_even(num1))
elif choice == '14':
print("Factorial is:", factorial(num1))
elif choice == '15':
print("Absolute value is:", absoulute(num1))
elif choice == '16':
print("Power:", power(num1, num2))
elif choice == '17':
print("Sin:", sin(num1))
elif choice == '18':
print("Cos:", cos(num1))
elif choice == '19':
print("Tan:", tan(num1))
elif choice == '20':
print("Radian:", radian(num1))
else:
print("Invalid input")

i am not able to calculate my answer and always gives answer err

def add(x, y):
return x + y
def sub(x, y):
return x - y
def multiply(x, y ):
return x * y
def div(x, y):
return x / y
print("Select Operation:")
print("1.add")
print("2.sub")
print("3.multiply")
print("4.div")
choice = input("enter your operation number: ")
a = int(input("enter the first number:"))
b = int(input("enter the second number: "))
if choice == 1:
print(a,"+",b,"=", add(a,b))
elif choice == 2:
print(a,"-",b,"=", sub(a,b))
elif choice == 3:
print(a,"*",b,"=", multiply(a,b))
elif choice == 4:
print(a,"/",b,"=", div(a,b))
else:
print("err")
Pay attention, when inputing something, it's a string.
So, when doing: choice == 1, it tries to do: '1' == 1, which is false
Do:
if choice == '1':
print(a,"+",b,"=", add(a,b))
elif choice == '2':
print(a,"-",b,"=", sub(a,b))
elif choice == '3':
print(a,"*",b,"=", multiply(a,b))
elif choice == '4':
# complete..

Categories