print("welcome to the Pythagoras calculator")
print("please leave empty whenever asked for any sides of the triangle you do not have data for")
print("please answer every question with integers only")
side = input("which side would you like to be found?")
hyp = int(input("hypotenuse length:"))
if hyp == (''):
hyp = str(hyp)
adj = int(input("adjacent length:"))
if adj ==(''):
adj = str(adj)
opp = int(input("opposite length:"))
if opp == (''):
opp = str(opp)
while hyp == ("") and adj == (""):
print("you need to insert two values")
hyp = int(input("hypotenuse length:"))
adj = int(input("adjacent length:"))
opp = int(input("opposite length:"))
while hyp == ("") and opp == (""):
print("you need to insert two values")
hyp = int(input("hypotenuse length:"))
adj = int(input("adjacent length:"))
opp = int(input("opposite length:"))
while adj == ("") and opp == (""):
print("you need to insert two values")
hyp = int(input("hypotenuse length:"))
adj = int(input("adjacent length:"))
opp = int(input("opposite length:"))
while adj == ("") and opp == (""):
print("you need to insert two values")
hyp = int(input("hypotenuse length:"))
adj = int(input("adjacent length:"))
opp = int(input("opposite length:"))
while hyp == ("") and adj == ("") and opp == (""):
print("you need to insert two values")
hyp = int(input("hypotenuse length:"))
adj = int(input("adjacent length:"))
opp = int(input("opposite length:"))
I'm trying to create a Pythagoras calculator yet when I ask people to insert the length of the sides it pops up an error which says that basically I'm trying to use an int as a string ( in validation), I'm aware that I can´t use an int as a string I just can´t figure out how to operate with both string and integers in the same input ( I ask for an input and it is both a string and an integer).
Thanks
In python 2.7 , you simply use raw_input()
like
hyp = raw_input("hypotenuse length:")
adj = raw_input("adjacent length:")
opp = raw_input("opposite length:")
And then your checks will work as they will be empty strings if nothing is entered.
Also you should preferably use raw_input
for all inputs, wherever you would need the value as int
you explicitly covert it then, like
temp = int(adj)
raw_input()
returns a string, and input()
tries to run the input as a Python expression.
In python 3, raw_input
is just renamed as input
and hence can be use directly to get input as string.
You could use a try
/ except
block as shown, and add additional conditions to validate for (I checked for number of blank sides in validate_input()
, but you could extend to positive numbers, etc).
#!/usr/bin/python
import math
#Triangle has three sides; two can be defined and the third is calculated
class Triangle:
def __init__(self):
self.side={"adjacent":0,"opposite":0,"hypotenuse":0}
def define_sides(self):
for i in self.side:
self.side[i]=self.get_side(i)
def print_sides(self):
for i in self.side:
print "side",i,"equals",self.side[i]
#return user integer or None if they enter nothing
def get_side(self,one_side):
prompt = "Enter length of "+one_side+": "
try:
return input(prompt)
except SyntaxError:
return None
def count_nones(self):
nones=0
for side, length in self.side.iteritems():
if self.side[side] == None:
nones+=1
return nones
def validate_input(self):
nNones=self.count_nones()
if nNones < 1:
print "You must leave one side blank."
quit()
if nNones > 1:
print "Only one side can be left blank."
def calculate_missing_length(self):
h=self.side["hypotenuse"]
a=self.side["adjacent"]
o=self.side["opposite"]
#hypotenuse is missing
if h == None:
self.side["hypotenuse"] = math.sqrt(a*a+o*o)
#adjacent is missing
if a == None:
self.side["adjacent"] = math.sqrt(h*h-o*o)
#opposite is missing
if o == None:
self.side["opposite"] = math.sqrt(h*h-a*a)
#begin program
triangle=Triangle()
triangle.define_sides()
triangle.print_sides()
triangle.validate_input()
triangle.calculate_missing_length()
triangle.print_sides()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.