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:"))
我正在尝试创建一个毕达哥拉斯计算器,但是当我要求人们插入边的长度时,它会弹出一个错误,说基本上我尝试使用int作为字符串(在验证中),我知道我不能使用int作为字符串,我只是不知道如何在同一个输入中同时使用字符串和整数(我要求)它是一个字符串和一个整数)。你知道吗
谢谢
您可以使用如图所示的
try
/except
块,并添加其他条件进行验证(我检查了validate_input()
中的空白边数,但可以扩展到正数,等等)。你知道吗在Python2.7中,您只需像
然后您的支票将工作,因为如果没有输入任何内容,它们将是空字符串。你知道吗
另外,您最好对所有输入使用
raw_input
,只要您需要值int
,然后显式地转换它,如raw_input()
返回一个字符串,input()
尝试将输入作为Python表达式运行。你知道吗在python3中,
raw_input
只是重命名为input
,因此可以直接使用它来获取字符串形式的输入。你知道吗相关问题 更多 >
编程相关推荐