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作爲字符串我只是不能弄清楚如何在同一個輸入中同時使用字符串和整數(我要求一個輸入,它既是一個字符串又是一個整數)。如何要求一個整數和一個字符串
感謝
'如果HYP ==(「」)'永遠不會發生,如果字符串是空的,你會得到一個錯誤,如果你想驗證輸入使用try /除外。 http://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response –
寫你自己的輸入函數,檢查用戶輸入的字符串的長度和使用try/except子句來查看它是否可以使用int()來轉換爲整數,然後用它代替int(input(「name:」))'。 – martineau