I am a beginner python learner. I created this simple program but won't display any error message neither does it work. After the input it stops working. What am I doing wrong? [ Python 3.2]
import math
print('''
|.
| .
a| . c
| .
|________.
b
''')
def robot():
a = float(input('Enter side a, 0 for unknown \n: '))
b = float(input('Enter side b, 0 for unknown \n: '))
c = float(input('Enter hypotenuse c, 0 for unknown \n: '))
if a == 0:
print = ('a = ', (math.sqrt((c**2)-(b**2))))
if b == 0:
print = ('b = ', (math.sqrt((c**2)-(a**2))))
if c == 0:
print = ('a = ', (math.sqrt((a**2)+(b**2))))
input()
robot()
robot()
Thanks
print = ('b = ', (math.sqrt((c**2)-(a**2))))
^
Delete the assignment operator after the print. print is a function, so to call it you only have to provide the arguments in the parenthesis, like this:
print('b = ', (math.sqrt((c**2)-(a**2))))
Related
While programming in Python I got stuck in a case where the while loop is not terminating even after the condition is being satisified then also
the code is as follows:
print('--- Alex\'s Calculator ---')
print('1. ADDition')
print('2. SUBstraction')
print('3. MULtiply')
print('4. DIVide')
print('5. EXIT')
x = int(input())
command = ' Enter Your Two numbers To Perform The Operation : '
def ini():
a = int(input())
b = int(input())
return a, b
def resultoo():
result = ' Your Result after Performing The Operation from {} and {} is {}'
print(result.format(a,b,c))
print(' Want To Continue If Yes then Enter Your Choice else Press any number exept 1 - 4')
x = int(input())
while x < 5:
if x == 1:
print(command)
a, b = ini()
c = a + b
resultoo()
elif x < 5:
break
As kuro specified in the comment, x can't be seen by your while loop because it's local to resultoo().
To solve it easily just add :
return x
at the end of resultoo()
and
x = resultoo()
in your while loop
You can use global var to this, change the this:
def resultoo():
result = ' Your Result after Performing The Operation from {} and {} is {}'
print(result.format(a,b,c))
print(' Want To Continue If Yes then Enter Your Choice else Press any number exept 1 - 4')
x = int(input())
into:
def resultoo():
global x
result = ' Your Result after Performing The Operation from {} and {} is {}'
print(result.format(a,b,c))
print(' Want To Continue If Yes then Enter Your Choice else Press any number exept 1 - 4')
x = int(input())
Explnation:
x is a global argument, that will be the same out of the function closure, but not inside of it, the function has it own params, so if you want to change a global argument that is initalizing outside the function, you will need to call the global statement before, that will make x the global x
When option 5 is entered you want to exit.
I added
import sys
and changed
elif x < 5:
to
elif x == 5:
and added
sys.exit(0)
I also added the getMenu() function
This is the complete code that is working in my editor:
import sys
def ini():
command = ' Enter Your Two numbers To Perform The Operation : '
print(command)
a = int(input())
b = int(input())
return a, b
def resultoo(a, b, c):
result = ' Your Result after Performing The Operation from {} and {} is {}'
print(result.format(a, b, c))
def getMenu(x):
if x == 0:
print("Choose menu item")
x = int(input())
elif x != 0:
print(' Want To Continue If Yes then Enter Your Choice else Press any number exept 1 - 4')
x = int(input())
return x
def main():
x = 0
while x < 5:
print('\n\n1. ADDition')
print('2. SUBstraction')
print('3. MULtiply')
print('4. DIVide')
print('5. EXIT\n')
x = getMenu(x)
if x == 1:
a, b = ini()
c = a + b
resultoo(a, b, c)
elif x == 5:
sys.exit(0)
else:
print("No valid menu item")
if __name__ == '__main__':
print('----------------------------------------------------------------------------------------------------------')
print('-------------------------------------------- Alex\'s Calculator -------------------------------------------')
main()
I also formatted your code (alt+Enter in Pycharm) to comply to PEP8 standards ;)
I started learning my first real language and can't find a solution for my problem:
how can I convert the user input, if someone is typing minus 10 for example instead of -10?
I just want to convert minus to -
I also added my code.
def get_temperatur():
minus = "-"
plus = "+"
while True:
C = input("Input temperature in Celsius: ")
try:
C = float(C)
return C
except ValueError:
print("That is not a valid input")
def convert_to_Kelvin(C):
K = C +273.15
return K
if __name__ == "__main__":
C = get_temperatur()
print("That is " + str(convert_to_Kelvin(C)) + " Kelvin")
def convert_to_Fahrenheit(C):
L = C * 1.8
F = L +32
return F
if __name__ == "__main__":
F = get_temperatur()
print("That is " + str(convert_to_Fahrenheit(F)) + " Fahrenheit")
I expect user input minus to be converted to -
Adding - sign will not make it a negative number, It is still going to be a string and you will get ValueError.
Instead do something like,
if 'minus' in c:
c = 0 - float(c.split('minus')[1].strip())
Note: I am assuming that your string will not contains any other word after number.
i.e. It will be like 'minus 10' and not 'minus 10 xyz'.
def get_temperatur():
minus = "-"
plus = "+"
while True:
C = input("Input temperature in Celsius: ")
try:
if 'minus ' in C:
C = C.replace('minus ', '-')
C = float(C)
return C
except ValueError:
print("That is not a valid input")
The C.replace() function takes in 2 values - the string to be replaced, and what it is to be replaced with. The if statement checks if 'minus '(note the space) is in the string, and if it is, replaces it with '-'
def get_temperatur():
while True:
C = input("Input temperature in Celsius: ")
C.replace("minus ", "-")
try:
C = float(C)
return C
except ValueError:
print("That is not a valid input")
I'm making an interest calculator that does compound and simple interest. However, the if statement always runs the simple interest script regardless of input.
I have tried changing variables to strings, integers, and floats. I have tried changing variable names, I have tried removing the first block of code entirely. What the heck is wrong with it???
start = input("simple or compound: ")
if start == "simple" or "Simple":
a = float(input('Starting balance: '))
b = float(input('Rate: '))
c = int(input('Years: '))
final = int(a+((a*b*c)/100))
print(final)
elif start == "compound" or "Compound":
d = float(input('Starting balance: '))
e = float(input('Rate: '))
f = int(input('Years: '))
final2 = int(d*(1+(e/100))**f)
print(final2)
else:
d = float(input('Starting balance: '))
e = float(input('Rate: '))
f = int(input('Years: '))
final3 = int(d*(1+(e/100))**f)
print(final3)
If I input Starting balance as 5000, rate as 5, and years as six into simple it gives 6500. But the same result occurs when I call compound.
This expression is not correct:
start == "simple" or "Simple"
should be
start == "simple" or start "Simple"
code below worked:
start = input("simple or compound: ")
if start == "simple" or start == "Simple":
# print("simple")
a = float(input('Starting balance: '))
b = float(input('Rate: '))
c = int(input('Years: '))
final = int(a+((a*b*c)/100))
print(final)
elif start == "compound" or start == "Compound":
# print("compound")
d = float(input('Starting balance: '))
e = float(input('Rate: '))
f = int(input('Years: '))
final2 = int(d*(1+(e/100))**f)
print(final2)
else:
# print("unknown")
d = float(input('Starting balance: '))
e = float(input('Rate: '))
f = int(input('Years: '))
final3 = int(d*(1+(e/100))**f)
print(final3)
Because of operator precedence
if start == "simple" or "Simple"
is evaluated as
if (start == "simple") or "Simple"
The (...) part is True if the user entered "simple", but the "Simple" part is always True.
# Kinematics clculator
print('Kinematics Calculator')
print('If either one of the three values(s,v,t) is not given then put its value = 1')
s = float(input('Enter your distance = '))
print(s)
t = float(input('Enter your time = '))
print(t)
v = float(input('Enter your velocity = '))
print(v)
if 'v == 1' :
print('velocity = '+ str(s/t))
elif 't == 1' :
print('time = '+ str(s/v))
else :
's == 1'
print('distance = '+ str(v*t))
Help me correct this code. Whenever I try to calculate anything else than "velocity" it always uses the first print command i.e
print('velocity = '+ str(s/t))
'v == 1' always evaluates to true, because it's a non-empty string. You should use
if v == 1:
print('velocity = '+ str(s/t))
elif t == 1:
print('time = '+ str(s/v))
else:
print('distance = '+ str(v*t))
Im new to programming and I wrote a program to solve different variables in an equation. I have "if" "elif" and "else" set up to solve for different parts of the equation. For some reason though, it will only solve for the first part (the "if" part) I'll copy and paste the program below.
import math
print 'A=Pert Calculator'
print ''
print 'Created by Triton Seibert'
print ''
Y = raw_input('What letter would you like to solve for?: ')
if Y == 'A' or 'a' or '1':
print 'Solving for A'
print ''
P = float(raw_input('Set value for P (initial investment):'))
e = 2.71828
print ''
r = float(raw_input('Set value for r (rate):'))
print ''
t = float(raw_input('Set value for t (time in years):'))
print ''
ert = e**(r*t)
answer = P*ert
print 'A equals:'
print answer
elif Y == 'P' or 'p' or '2':
print 'Solving for P'
print ''
A = float(raw_input('Set value for A (Final answer):'))
e = 2.71828
print ''
r = float(raw_input('Set value for r (rate):'))
print ''
t = float(raw_input('Set value for t (time in years):'))
print ''
answer = A / math.e**(r*t)
print 'P equals:'
print answer
elif Y == 'R' or 'r' or '3':
print 'Solving for r'
print ' '
A = float(raw_input('Set value for A (Final answer): '))
P = float(raw_input('Set value for P (initial investment):'))
e = 2.71828
print ' '
t = float(raw_input('Set value for t (time in years):'))
print ' '
almost = A/P
getting_there = math.log10(almost)/math.log10(e)
answer = getting_there/t
print 'r equals:'
print answer
elif Y == 'T' or 't' or '4':
print 'Solving for t'
print ' '
A = float(raw_input('Set value for A (Final answer): '))
P = float(raw_input('Set value for P (initial investment):'))
e = 2.71828
print ' '
r = float(raw_input('Set value for r (rate):'))
print ' '
#equation here (not done yet)
print 't equals:'
print answer
else:
print 'Not yet'
#change log to ln : log base e (x) = log base 10 (x) / log base 10 (e)
This part always evaluates to True:
if Y == 'A' or 'a' or '1':
It's not doing what you think it's doing; it's doing this:
if (Y == 'A') or ('a') or ('1'):
and 'a' evaluates to True, so it passes. What you probably want is:
if Y in ['A', 'a', '1']: