【发布时间】:2016-04-10 23:57:41
【问题描述】:
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 用作字符串我只是不知道如何在同一输入中同时使用字符串和整数(我要求输入,它既是字符串又是整数)。
谢谢
【问题讨论】:
-
if hyp == ('')永远不会发生,如果字符串为空你会得到一个错误,如果你想验证输入使用 try/except。 stackoverflow.com/questions/23294658/… -
编写您自己的输入函数,检查用户输入的字符串的长度,并使用 try/except 子句查看是否可以使用
int()将其转换为整数 — 然后在任何地方使用它代替int(input("name:"))。